<h4 dir="auto">Code Sample</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np
import time

n_rows = 1000
n_categories = 250

df = pd.DataFrame({'cat1': np.random.choice(np.arange(0,n_categories), size=n_rows, replace=True),
                   'cat2': np.random.choice(np.arange(0,n_categories), size=n_rows, replace=True),
                   'cat3': np.random.choice(np.arange(0,n_categories), size=n_rows, replace=True),
                   'real_numbers': np.random.normal(size=n_rows)})

# Case 1: Pivot table without multi-index columns
now = time.time()
pd.pivot_table(df, values='real_numbers', index='cat1', columns=['cat2'])
print(&quot;Elapsed time:&quot;, time.time() - now)

# Case 2: Pivot table with multi-index columns
now = time.time()
pd.pivot_table(df, values='real_numbers', index='cat1', columns=['cat2', 'cat3'])
print(&quot;Elapsed time:&quot;, time.time() - now)

# Typecast a feature to categorical.
df_cast = df.astype({'cat2': 'category'})

# Case 3: Pivot table without multi-index columns - still OK
now = time.time()
pd.pivot_table(df_cast, values='real_numbers', index=['cat1'], columns=['cat2'])
print(&quot;Elapsed time:&quot;, time.time() - now)

# Case 4: Pivot table with multi-index columns - strong increase in elapsed time/ memory footprint.
now = time.time()
pd.pivot_table(df_cast, values='real_numbers', index=['cat1'], columns=['cat2', 'cat3'])
print(&quot;Elapsed time:&quot;, time.time() - now)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">time</span>

<span class="pl-s1">n_rows</span> <span class="pl-c1">=</span> <span class="pl-c1">1000</span>
<span class="pl-s1">n_categories</span> <span class="pl-c1">=</span> <span class="pl-c1">250</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'cat1'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">choice</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>,<span class="pl-s1">n_categories</span>), <span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-s1">n_rows</span>, <span class="pl-s1">replace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>),
                   <span class="pl-s">'cat2'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">choice</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>,<span class="pl-s1">n_categories</span>), <span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-s1">n_rows</span>, <span class="pl-s1">replace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>),
                   <span class="pl-s">'cat3'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">choice</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>,<span class="pl-s1">n_categories</span>), <span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-s1">n_rows</span>, <span class="pl-s1">replace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>),
                   <span class="pl-s">'real_numbers'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-s1">n_rows</span>)})

<span class="pl-c"># Case 1: Pivot table without multi-index columns</span>
<span class="pl-s1">now</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-s1">pd</span>.<span class="pl-en">pivot_table</span>(<span class="pl-s1">df</span>, <span class="pl-s1">values</span><span class="pl-c1">=</span><span class="pl-s">'real_numbers'</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s">'cat1'</span>, <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'cat2'</span>])
<span class="pl-en">print</span>(<span class="pl-s">"Elapsed time:"</span>, <span class="pl-s1">time</span>.<span class="pl-en">time</span>() <span class="pl-c1">-</span> <span class="pl-s1">now</span>)

<span class="pl-c"># Case 2: Pivot table with multi-index columns</span>
<span class="pl-s1">now</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-s1">pd</span>.<span class="pl-en">pivot_table</span>(<span class="pl-s1">df</span>, <span class="pl-s1">values</span><span class="pl-c1">=</span><span class="pl-s">'real_numbers'</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s">'cat1'</span>, <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'cat2'</span>, <span class="pl-s">'cat3'</span>])
<span class="pl-en">print</span>(<span class="pl-s">"Elapsed time:"</span>, <span class="pl-s1">time</span>.<span class="pl-en">time</span>() <span class="pl-c1">-</span> <span class="pl-s1">now</span>)

<span class="pl-c"># Typecast a feature to categorical.</span>
<span class="pl-s1">df_cast</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">astype</span>({<span class="pl-s">'cat2'</span>: <span class="pl-s">'category'</span>})

<span class="pl-c"># Case 3: Pivot table without multi-index columns - still OK</span>
<span class="pl-s1">now</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-s1">pd</span>.<span class="pl-en">pivot_table</span>(<span class="pl-s1">df_cast</span>, <span class="pl-s1">values</span><span class="pl-c1">=</span><span class="pl-s">'real_numbers'</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-s">'cat1'</span>], <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'cat2'</span>])
<span class="pl-en">print</span>(<span class="pl-s">"Elapsed time:"</span>, <span class="pl-s1">time</span>.<span class="pl-en">time</span>() <span class="pl-c1">-</span> <span class="pl-s1">now</span>)

<span class="pl-c"># Case 4: Pivot table with multi-index columns - strong increase in elapsed time/ memory footprint.</span>
<span class="pl-s1">now</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-s1">pd</span>.<span class="pl-en">pivot_table</span>(<span class="pl-s1">df_cast</span>, <span class="pl-s1">values</span><span class="pl-c1">=</span><span class="pl-s">'real_numbers'</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-s">'cat1'</span>], <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'cat2'</span>, <span class="pl-s">'cat3'</span>])
<span class="pl-en">print</span>(<span class="pl-s">"Elapsed time:"</span>, <span class="pl-s1">time</span>.<span class="pl-en">time</span>() <span class="pl-c1">-</span> <span class="pl-s1">now</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Creating a pivot table (with a multi-index) of a relatively small data frame with integer and float columns (case 2) goes much faster and uses much less resources compared to when the pivot table is created of the same data frame, but with one of the columns converted to a category (case 4).</p>
<p dir="auto">On my (fairly old) system I find roughly a 200x increase in elapsed time between cases 2 and 4 from the code sample. Furthermore, memory consumption in case 2 is negligible, while in case 4 it runs well over 2GB. Increasing the number of categories <code class="notranslate">n_categories</code> in the original data frame the increases the resource usage/ execution time further.</p>
<p dir="auto">On the other hand, when no multi-index is used, there only seems to be a very small drop in performance (e.g. from case 1 to case 3).</p>
<p dir="auto">There are already various issues submitted that involve memory usage of the pivot_table function, however as far as I see for large data frames. This issue seems different as it happens with relatively small data frames, and only in the specific case of a categorical variable in a multi-index.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I am unaware of the implementation details, but I would assume that a categorical variable has an integer representation under the hood and I would therefore expect no performance penalty at all, regardless of the number of columns chosen.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
<p dir="auto">pandas: 0.23.0.dev0+259.g7dcc86443<br>
pytest: 3.3.2<br>
pip: 9.0.1<br>
setuptools: 38.4.0<br>
Cython: 0.27.3<br>
numpy: 1.14.0<br>
scipy: 1.0.0<br>
pyarrow: 0.8.0<br>
xarray: None<br>
IPython: 6.2.1<br>
sphinx: 1.6.6<br>
patsy: 0.5.0<br>
dateutil: 2.6.1<br>
pytz: 2017.3<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.2<br>
numexpr: 2.6.4<br>
feather: None<br>
matplotlib: 2.1.2<br>
openpyxl: 2.4.10<br>
xlrd: 1.1.0<br>
xlwt: 1.2.0<br>
xlsxwriter: 1.0.2<br>
lxml: 4.1.1<br>
bs4: 4.6.0<br>
html5lib: 1.0.1<br>
sqlalchemy: 1.2.1<br>
pymysql: 0.7.11.None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: 0.1.2<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(10,100, size=(200,6)), columns=['C'+str(i) for i in range(6)])
df['C0'] = ['A','B','C','D']*50
df['C1'] = ['E','F']*100
df['C2'] = ['H','I','J','K', 'L']*40

for col in df.columns[:3]:
    df[col] = df[col].astype('category')

df.info()

&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 200 entries, 0 to 199
Data columns (total 6 columns):
C0    200 non-null category
C1    200 non-null category
C2    200 non-null category
C3    200 non-null int32
C4    200 non-null int32
C5    200 non-null int32
dtypes: category(3), int32(3)
memory usage: 3.1 KB

%time ix_true = df.groupby(df.columns.tolist()[:3], as_index=True)['C5'].max()
Wall time: 2 ms

ix_true.shape
(20,)

%time ix_false = df.groupby(df.columns.tolist()[:3], as_index=False)['C5'].max()
Wall time: 15 ms

ix_false.shape
(40, 4)

ix_true
C0  C1  C2
A   E   H     93
        I     99
        J     88
        K     91
        L     94
B   F   H     98
        I     89
        J     94
        K     92
        L     96
C   E   H     96
        I     96
        J     85
        K     88
        L     98
D   F   H     96
        I     84
        J     71
        K     96
        L     94
Name: C5, dtype: int32

ix_false
	C0	C1	C2	C5
0	A	E	H	93.0
1	A	E	I	99.0
2	A	E	J	88.0
3	A	E	K	91.0
4	A	E	L	94.0
5	A	F	H	NaN
6	A	F	I	NaN
7	A	F	J	NaN
8	A	F	K	NaN
9	A	F	L	NaN
10	B	E	H	NaN
11	B	E	I	NaN
12	B	E	J	NaN
13	B	E	K	NaN
14	B	E	L	NaN
15	B	F	H	98.0
16	B	F	I	89.0
17	B	F	J	94.0
18	B	F	K	92.0
19	B	F	L	96.0
20	C	E	H	96.0
21	C	E	I	96.0
22	C	E	J	85.0
23	C	E	K	88.0
24	C	E	L	98.0
25	C	F	H	NaN
26	C	F	I	NaN
27	C	F	J	NaN
28	C	F	K	NaN
29	C	F	L	NaN
30	D	E	H	NaN
31	D	E	I	NaN
32	D	E	J	NaN
33	D	E	K	NaN
34	D	E	L	NaN
35	D	F	H	96.0
36	D	F	I	84.0
37	D	F	J	71.0
38	D	F	K	96.0
39	D	F	L	94.0
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">10</span>,<span class="pl-c1">100</span>, <span class="pl-s1">size</span><span class="pl-c1">=</span>(<span class="pl-c1">200</span>,<span class="pl-c1">6</span>)), <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'C'</span><span class="pl-c1">+</span><span class="pl-en">str</span>(<span class="pl-s1">i</span>) <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">6</span>)])
<span class="pl-s1">df</span>[<span class="pl-s">'C0'</span>] <span class="pl-c1">=</span> [<span class="pl-s">'A'</span>,<span class="pl-s">'B'</span>,<span class="pl-s">'C'</span>,<span class="pl-s">'D'</span>]<span class="pl-c1">*</span><span class="pl-c1">50</span>
<span class="pl-s1">df</span>[<span class="pl-s">'C1'</span>] <span class="pl-c1">=</span> [<span class="pl-s">'E'</span>,<span class="pl-s">'F'</span>]<span class="pl-c1">*</span><span class="pl-c1">100</span>
<span class="pl-s1">df</span>[<span class="pl-s">'C2'</span>] <span class="pl-c1">=</span> [<span class="pl-s">'H'</span>,<span class="pl-s">'I'</span>,<span class="pl-s">'J'</span>,<span class="pl-s">'K'</span>, <span class="pl-s">'L'</span>]<span class="pl-c1">*</span><span class="pl-c1">40</span>

<span class="pl-k">for</span> <span class="pl-s1">col</span> <span class="pl-c1">in</span> <span class="pl-s1">df</span>.<span class="pl-s1">columns</span>[:<span class="pl-c1">3</span>]:
    <span class="pl-s1">df</span>[<span class="pl-s1">col</span>] <span class="pl-c1">=</span> <span class="pl-s1">df</span>[<span class="pl-s1">col</span>].<span class="pl-en">astype</span>(<span class="pl-s">'category'</span>)

<span class="pl-s1">df</span>.<span class="pl-en">info</span>()

<span class="pl-c1">&lt;</span><span class="pl-k">class</span> <span class="pl-s">'pandas.core.frame.DataFrame'</span><span class="pl-c1">&gt;</span>
<span class="pl-v">RangeIndex</span>: <span class="pl-c1">200</span> <span class="pl-s1">entries</span>, <span class="pl-c1">0</span> <span class="pl-s1">to</span> <span class="pl-c1">199</span>
<span class="pl-v">Data</span> <span class="pl-en">columns</span> (<span class="pl-s1">total</span> <span class="pl-c1">6</span> <span class="pl-s1">columns</span>):
<span class="pl-v">C0</span>    <span class="pl-c1">200</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">category</span>
<span class="pl-v">C1</span>    <span class="pl-c1">200</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">category</span>
<span class="pl-v">C2</span>    <span class="pl-c1">200</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">category</span>
<span class="pl-v">C3</span>    <span class="pl-c1">200</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">int32</span>
<span class="pl-v">C4</span>    <span class="pl-c1">200</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">int32</span>
<span class="pl-v">C5</span>    <span class="pl-c1">200</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">int32</span>
<span class="pl-s1">dtypes</span>: <span class="pl-en">category</span>(<span class="pl-c1">3</span>), <span class="pl-en">int32</span>(<span class="pl-c1">3</span>)
<span class="pl-s1">memory</span> <span class="pl-s1">usage</span>: <span class="pl-c1">3.1</span> <span class="pl-v">KB</span>

<span class="pl-c1">%</span><span class="pl-s1">time</span> <span class="pl-s1">ix_true</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s1">df</span>.<span class="pl-s1">columns</span>.<span class="pl-en">tolist</span>()[:<span class="pl-c1">3</span>], <span class="pl-s1">as_index</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)[<span class="pl-s">'C5'</span>].<span class="pl-en">max</span>()
<span class="pl-v">Wall</span> <span class="pl-s1">time</span>: <span class="pl-c1">2</span> <span class="pl-s1">ms</span>

<span class="pl-s1">ix_true</span>.<span class="pl-s1">shape</span>
(<span class="pl-c1">20</span>,)

<span class="pl-c1">%</span><span class="pl-s1">time</span> <span class="pl-s1">ix_false</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s1">df</span>.<span class="pl-s1">columns</span>.<span class="pl-en">tolist</span>()[:<span class="pl-c1">3</span>], <span class="pl-s1">as_index</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)[<span class="pl-s">'C5'</span>].<span class="pl-en">max</span>()
<span class="pl-v">Wall</span> <span class="pl-s1">time</span>: <span class="pl-c1">15</span> <span class="pl-s1">ms</span>

<span class="pl-s1">ix_false</span>.<span class="pl-s1">shape</span>
(<span class="pl-c1">40</span>, <span class="pl-c1">4</span>)

<span class="pl-s1">ix_true</span>
<span class="pl-v">C0</span>  <span class="pl-v">C1</span>  <span class="pl-v">C2</span>
<span class="pl-v">A</span>   <span class="pl-v">E</span>   <span class="pl-v">H</span>     <span class="pl-c1">93</span>
        <span class="pl-v">I</span>     <span class="pl-c1">99</span>
        <span class="pl-v">J</span>     <span class="pl-c1">88</span>
        <span class="pl-v">K</span>     <span class="pl-c1">91</span>
        <span class="pl-v">L</span>     <span class="pl-c1">94</span>
<span class="pl-v">B</span>   <span class="pl-v">F</span>   <span class="pl-v">H</span>     <span class="pl-c1">98</span>
        <span class="pl-v">I</span>     <span class="pl-c1">89</span>
        <span class="pl-v">J</span>     <span class="pl-c1">94</span>
        <span class="pl-v">K</span>     <span class="pl-c1">92</span>
        <span class="pl-v">L</span>     <span class="pl-c1">96</span>
<span class="pl-v">C</span>   <span class="pl-v">E</span>   <span class="pl-v">H</span>     <span class="pl-c1">96</span>
        <span class="pl-v">I</span>     <span class="pl-c1">96</span>
        <span class="pl-v">J</span>     <span class="pl-c1">85</span>
        <span class="pl-v">K</span>     <span class="pl-c1">88</span>
        <span class="pl-v">L</span>     <span class="pl-c1">98</span>
<span class="pl-v">D</span>   <span class="pl-v">F</span>   <span class="pl-v">H</span>     <span class="pl-c1">96</span>
        <span class="pl-v">I</span>     <span class="pl-c1">84</span>
        <span class="pl-v">J</span>     <span class="pl-c1">71</span>
        <span class="pl-v">K</span>     <span class="pl-c1">96</span>
        <span class="pl-v">L</span>     <span class="pl-c1">94</span>
<span class="pl-v">Name</span>: <span class="pl-v">C5</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">int32</span>

<span class="pl-s1">ix_false</span>
	<span class="pl-v">C0</span>	<span class="pl-v">C1</span>	<span class="pl-v">C2</span>	<span class="pl-v">C5</span>
<span class="pl-c1">0</span>	<span class="pl-v">A</span>	<span class="pl-v">E</span>	<span class="pl-v">H</span>	<span class="pl-c1">93.0</span>
<span class="pl-c1">1</span>	<span class="pl-v">A</span>	<span class="pl-v">E</span>	<span class="pl-v">I</span>	<span class="pl-c1">99.0</span>
<span class="pl-c1">2</span>	<span class="pl-v">A</span>	<span class="pl-v">E</span>	<span class="pl-v">J</span>	<span class="pl-c1">88.0</span>
<span class="pl-c1">3</span>	<span class="pl-v">A</span>	<span class="pl-v">E</span>	<span class="pl-v">K</span>	<span class="pl-c1">91.0</span>
<span class="pl-c1">4</span>	<span class="pl-v">A</span>	<span class="pl-v">E</span>	<span class="pl-v">L</span>	<span class="pl-c1">94.0</span>
<span class="pl-c1">5</span>	<span class="pl-v">A</span>	<span class="pl-v">F</span>	<span class="pl-v">H</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">6</span>	<span class="pl-v">A</span>	<span class="pl-v">F</span>	<span class="pl-v">I</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">7</span>	<span class="pl-v">A</span>	<span class="pl-v">F</span>	<span class="pl-v">J</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">8</span>	<span class="pl-v">A</span>	<span class="pl-v">F</span>	<span class="pl-v">K</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">9</span>	<span class="pl-v">A</span>	<span class="pl-v">F</span>	<span class="pl-v">L</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">10</span>	<span class="pl-v">B</span>	<span class="pl-v">E</span>	<span class="pl-v">H</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">11</span>	<span class="pl-v">B</span>	<span class="pl-v">E</span>	<span class="pl-v">I</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">12</span>	<span class="pl-v">B</span>	<span class="pl-v">E</span>	<span class="pl-v">J</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">13</span>	<span class="pl-v">B</span>	<span class="pl-v">E</span>	<span class="pl-v">K</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">14</span>	<span class="pl-v">B</span>	<span class="pl-v">E</span>	<span class="pl-v">L</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">15</span>	<span class="pl-v">B</span>	<span class="pl-v">F</span>	<span class="pl-v">H</span>	<span class="pl-c1">98.0</span>
<span class="pl-c1">16</span>	<span class="pl-v">B</span>	<span class="pl-v">F</span>	<span class="pl-v">I</span>	<span class="pl-c1">89.0</span>
<span class="pl-c1">17</span>	<span class="pl-v">B</span>	<span class="pl-v">F</span>	<span class="pl-v">J</span>	<span class="pl-c1">94.0</span>
<span class="pl-c1">18</span>	<span class="pl-v">B</span>	<span class="pl-v">F</span>	<span class="pl-v">K</span>	<span class="pl-c1">92.0</span>
<span class="pl-c1">19</span>	<span class="pl-v">B</span>	<span class="pl-v">F</span>	<span class="pl-v">L</span>	<span class="pl-c1">96.0</span>
<span class="pl-c1">20</span>	<span class="pl-v">C</span>	<span class="pl-v">E</span>	<span class="pl-v">H</span>	<span class="pl-c1">96.0</span>
<span class="pl-c1">21</span>	<span class="pl-v">C</span>	<span class="pl-v">E</span>	<span class="pl-v">I</span>	<span class="pl-c1">96.0</span>
<span class="pl-c1">22</span>	<span class="pl-v">C</span>	<span class="pl-v">E</span>	<span class="pl-v">J</span>	<span class="pl-c1">85.0</span>
<span class="pl-c1">23</span>	<span class="pl-v">C</span>	<span class="pl-v">E</span>	<span class="pl-v">K</span>	<span class="pl-c1">88.0</span>
<span class="pl-c1">24</span>	<span class="pl-v">C</span>	<span class="pl-v">E</span>	<span class="pl-v">L</span>	<span class="pl-c1">98.0</span>
<span class="pl-c1">25</span>	<span class="pl-v">C</span>	<span class="pl-v">F</span>	<span class="pl-v">H</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">26</span>	<span class="pl-v">C</span>	<span class="pl-v">F</span>	<span class="pl-v">I</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">27</span>	<span class="pl-v">C</span>	<span class="pl-v">F</span>	<span class="pl-v">J</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">28</span>	<span class="pl-v">C</span>	<span class="pl-v">F</span>	<span class="pl-v">K</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">29</span>	<span class="pl-v">C</span>	<span class="pl-v">F</span>	<span class="pl-v">L</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">30</span>	<span class="pl-v">D</span>	<span class="pl-v">E</span>	<span class="pl-v">H</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">31</span>	<span class="pl-v">D</span>	<span class="pl-v">E</span>	<span class="pl-v">I</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">32</span>	<span class="pl-v">D</span>	<span class="pl-v">E</span>	<span class="pl-v">J</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">33</span>	<span class="pl-v">D</span>	<span class="pl-v">E</span>	<span class="pl-v">K</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">34</span>	<span class="pl-v">D</span>	<span class="pl-v">E</span>	<span class="pl-v">L</span>	<span class="pl-v">NaN</span>
<span class="pl-c1">35</span>	<span class="pl-v">D</span>	<span class="pl-v">F</span>	<span class="pl-v">H</span>	<span class="pl-c1">96.0</span>
<span class="pl-c1">36</span>	<span class="pl-v">D</span>	<span class="pl-v">F</span>	<span class="pl-v">I</span>	<span class="pl-c1">84.0</span>
<span class="pl-c1">37</span>	<span class="pl-v">D</span>	<span class="pl-v">F</span>	<span class="pl-v">J</span>	<span class="pl-c1">71.0</span>
<span class="pl-c1">38</span>	<span class="pl-v">D</span>	<span class="pl-v">F</span>	<span class="pl-v">K</span>	<span class="pl-c1">96.0</span>
<span class="pl-c1">39</span>	<span class="pl-v">D</span>	<span class="pl-v">F</span>	<span class="pl-v">L</span>	<span class="pl-c1">94.0</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Using <code class="notranslate">as_index=False</code> in <code class="notranslate">df.groupby(df.columns.tolist()[:3], as_index=False)['C5'].max()</code> with categorical columns produces NaN output rows.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I expect that the output should not contain any extra Cardinality explosion and have the same number of rows. as the <code class="notranslate">as_index=True</code> case.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
<p dir="auto">pandas: 0.19.2<br>
nose: 1.3.7<br>
pip: 8.1.1<br>
setuptools: 20.10.1<br>
Cython: 0.24.1<br>
numpy: 1.11.2<br>
scipy: 0.18.1<br>
statsmodels: 0.6.1<br>
xarray: 0.7.1<br>
IPython: 4.2.0<br>
sphinx: 1.3.6<br>
patsy: 0.4.0<br>
dateutil: 2.5.0<br>
pytz: 2015.7<br>
blosc: None<br>
bottleneck: 1.0.0<br>
tables: 3.3.0<br>
numexpr: 2.6.1<br>
matplotlib: 1.5.1<br>
openpyxl: 2.2.5<br>
xlrd: 0.9.4<br>
xlwt: 1.0.0<br>
xlsxwriter: None<br>
lxml: 3.6.4<br>
bs4: 4.4.1<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.0.12<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: None<br>
pandas_datareader: None</p>
</details>