Issue 1:
Code Sample, a copy-pastable example if possible

# 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))

Problem description

Here is the output of the above code

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

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).

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).

Expected Output

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

Output of pd.show_versions()

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

pandas: 0.20.2

pytest: None

pip: 9.0.1

setuptools: 27.2.0

Cython: None

numpy: 1.13.1

scipy: 0.19.1

xarray: None

IPython: 6.1.0

sphinx: None

patsy: None

dateutil: 2.6.0

pytz: 2017.2

blosc: None

bottleneck: None

tables: None

numexpr: None

feather: None

matplotlib: 2.0.2

openpyxl: None

xlrd: None

xlwt: None

xlsxwriter: None

lxml: None

bs4: None

html5lib: 0.999

sqlalchemy: 1.1.11

pymysql: None

psycopg2: None

jinja2: 2.9.6

s3fs: None

pandas_gbq: None

pandas_datareader: None

Issue 2:
I have checked that this issue has not already been reported.

I have confirmed this bug exists on the latest version of pandas.

(optional) I have confirmed this bug exists on the master branch of pandas.

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

df = pd.DataFrame(
{
"col1": [0, 1, 2, 3],
"col4": [17, 13, 16, 15],
"col5": [-4, -5, -6, -7],
}
)
by=["col4", "col5"]
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)

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

In the code above two calls to gb.apply(apply_function) produce different output. The reason for this is that groupby.min is called before 2nd apply and makes its output different and incorrect.

Expected Output

Expected that both calls to gb.apply(apply_function) produce the same output.

Output of pd.show_versions()

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

pandas : 1.0.4

numpy : 1.18.4

pytz : 2019.2

dateutil : 2.7.3

pip : 20.1.1

setuptools : 47.1.0

Cython : 0.29.17

pytest : 5.4.2

hypothesis : None

sphinx : None

blosc : None

feather : 0.4.1

xlsxwriter : None

lxml.etree : 4.5.1

html5lib : None

pymysql : None

psycopg2 : None

jinja2 : 2.11.2

IPython : 7.12.0

pandas_datareader: None

bs4 : 4.8.2

bottleneck : None

fastparquet : None

gcsfs : None

lxml.etree : 4.5.1

matplotlib : 3.2.1

numexpr : 2.7.1

odfpy : None

openpyxl : 3.0.3

pandas_gbq : 0.13.2

pyarrow : 0.16.0

pytables : None

pytest : 5.4.2

pyxlsb : None

s3fs : 0.4.2

scipy : 1.4.1

sqlalchemy : 1.3.17

tables : 3.6.1

tabulate : None

xarray : 0.15.1

xlrd : 1.2.0

xlwt : None

xlsxwriter : None

numba : 0.46.0