<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="# I create a test DataFrame
d = pd.DataFrame({'data': range(6), 'key': list('ABCABC')})
# I groupby the column 'key'
g = d.groupby('key')
# I filter with always True (not that useful, just for the example)
print(g.filter(lambda x: True))
g.sum()
print(g.filter(lambda x: True))
"><pre class="notranslate"><span class="pl-c"># I create a test DataFrame</span>
<span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'data'</span>: <span class="pl-en">range</span>(<span class="pl-c1">6</span>), <span class="pl-s">'key'</span>: <span class="pl-en">list</span>(<span class="pl-s">'ABCABC'</span>)})
<span class="pl-c"># I groupby the column 'key'</span>
<span class="pl-s1">g</span> <span class="pl-c1">=</span> <span class="pl-s1">d</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'key'</span>)
<span class="pl-c"># I filter with always True (not that useful, just for the example)</span>
<span class="pl-en">print</span>(<span class="pl-s1">g</span>.<span class="pl-en">filter</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-c1">True</span>))
<span class="pl-s1">g</span>.<span class="pl-en">sum</span>()
<span class="pl-en">print</span>(<span class="pl-s1">g</span>.<span class="pl-en">filter</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-c1">True</span>))</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Here is the output of the above code</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="   data key
0     0   A
1     1   B
2     2   C
3     3   A
4     4   B
5     5   C
   data
0     0
1     1
2     2
3     3
4     4
5     5"><pre class="notranslate"><code class="notranslate">   data key
0     0   A
1     1   B
2     2   C
3     3   A
4     4   B
5     5   C
   data
0     0
1     1
2     2
3     3
4     4
5     5
</code></pre></div>
<p dir="auto">I don't understand why the column key is in the output in the first run of filter, whereas when running an aggregate function (here g.sum()) before the filter, the key column disappear. If I use the as_index=False for the groupby, then the column is correctly preserved (as expected).</p>
<p dir="auto">It looks like the aggregate function somehow change the groupby object, whereas my understanding of the groupby object is that each function call return a new object (and do not modify the original groupby object).</p>
<h4 dir="auto">Expected Output</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="   data key
0     0   A
1     1   B
2     2   C
3     3   A
4     4   B
5     5   C

   data key
0     0   A
1     1   B
2     2   C
3     3   A
4     4   B
5     5   C
"><pre class="notranslate"><code class="notranslate">   data key
0     0   A
1     1   B
2     2   C
3     3   A
4     4   B
5     5   C

   data key
0     0   A
1     1   B
2     2   C
3     3   A
4     4   B
5     5   C

</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
<p dir="auto">pandas: 0.20.2<br>
pytest: None<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: None<br>
numpy: 1.13.1<br>
scipy: 0.19.1<br>
xarray: None<br>
IPython: 6.1.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.0<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: 2.0.2<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: 0.999<br>
sqlalchemy: 1.1.11<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<ul class="contains-task-list">
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked that this issue has not already been reported.</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have confirmed this bug exists on the latest version of pandas.</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> (optional) I have confirmed this bug exists on the master branch of pandas.</p>
</li>
</ul>
<hr>
<p dir="auto"><strong>Note</strong>: Please read <a href="https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports" rel="nofollow">this guide</a> detailing how to provide the necessary information for us to reproduce your bug.</p>
<h4 dir="auto">Code Sample, a copy-pastable example</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd

df = pd.DataFrame(
    {
        &quot;col1&quot;: [0, 1, 2, 3],
        &quot;col4&quot;: [17, 13, 16, 15],
        &quot;col5&quot;: [-4, -5, -6, -7],
    }
)
by=[&quot;col4&quot;, &quot;col5&quot;]
apply_function = min

gb = df.groupby(by, as_index=True)

df1 = gb.apply(apply_function)
print(df1)

df2 = gb.min()
print(df2)

df3 = gb.apply(apply_function)
print(df3)"><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-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(
    {
        <span class="pl-s">"col1"</span>: [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>],
        <span class="pl-s">"col4"</span>: [<span class="pl-c1">17</span>, <span class="pl-c1">13</span>, <span class="pl-c1">16</span>, <span class="pl-c1">15</span>],
        <span class="pl-s">"col5"</span>: [<span class="pl-c1">-</span><span class="pl-c1">4</span>, <span class="pl-c1">-</span><span class="pl-c1">5</span>, <span class="pl-c1">-</span><span class="pl-c1">6</span>, <span class="pl-c1">-</span><span class="pl-c1">7</span>],
    }
)
<span class="pl-s1">by</span><span class="pl-c1">=</span>[<span class="pl-s">"col4"</span>, <span class="pl-s">"col5"</span>]
<span class="pl-s1">apply_function</span> <span class="pl-c1">=</span> <span class="pl-s1">min</span>

<span class="pl-s1">gb</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s1">by</span>, <span class="pl-s1">as_index</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">gb</span>.<span class="pl-en">apply</span>(<span class="pl-s1">apply_function</span>)
<span class="pl-en">print</span>(<span class="pl-s1">df1</span>)

<span class="pl-s1">df2</span> <span class="pl-c1">=</span> <span class="pl-s1">gb</span>.<span class="pl-en">min</span>()
<span class="pl-en">print</span>(<span class="pl-s1">df2</span>)

<span class="pl-s1">df3</span> <span class="pl-c1">=</span> <span class="pl-s1">gb</span>.<span class="pl-en">apply</span>(<span class="pl-s1">apply_function</span>)
<span class="pl-en">print</span>(<span class="pl-s1">df3</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">[this should explain <strong>why</strong> the current behaviour is a problem and why the expected output is a better solution]</p>
<p dir="auto">In the code above two calls to <code class="notranslate">gb.apply(apply_function)</code> produce different output. The reason for this is that <code class="notranslate">groupby.min</code> is called before 2nd <code class="notranslate">apply</code> and makes its output different and incorrect.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">Expected that both calls to <code class="notranslate">gb.apply(apply_function)</code> produce the same output.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.5.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.3.0-26-generic
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8
<p dir="auto">pandas           : 1.0.4<br>
numpy            : 1.18.4<br>
pytz             : 2019.2<br>
dateutil         : 2.7.3<br>
pip              : 20.1.1<br>
setuptools       : 47.1.0<br>
Cython           : 0.29.17<br>
pytest           : 5.4.2<br>
hypothesis       : None<br>
sphinx           : None<br>
blosc            : None<br>
feather          : 0.4.1<br>
xlsxwriter       : None<br>
lxml.etree       : 4.5.1<br>
html5lib         : None<br>
pymysql          : None<br>
psycopg2         : None<br>
jinja2           : 2.11.2<br>
IPython          : 7.12.0<br>
pandas_datareader: None<br>
bs4              : 4.8.2<br>
bottleneck       : None<br>
fastparquet      : None<br>
gcsfs            : None<br>
lxml.etree       : 4.5.1<br>
matplotlib       : 3.2.1<br>
numexpr          : 2.7.1<br>
odfpy            : None<br>
openpyxl         : 3.0.3<br>
pandas_gbq       : 0.13.2<br>
pyarrow          : 0.16.0<br>
pytables         : None<br>
pytest           : 5.4.2<br>
pyxlsb           : None<br>
s3fs             : 0.4.2<br>
scipy            : 1.4.1<br>
sqlalchemy       : 1.3.17<br>
tables           : 3.6.1<br>
tabulate         : None<br>
xarray           : 0.15.1<br>
xlrd             : 1.2.0<br>
xlwt             : None<br>
xlsxwriter       : None<br>
numba            : 0.46.0</p>
</details>