Description
Problem description
When using a rolling agg function on a groupby object, we cannot ommit the groupby columns from the resulting dataframe's index using as_index = False
, like we can when applying a non-rolling agg function.
Code Sample
import pandas as pd
data = {
'groupby_col': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', ],
'agg_col': [1, 1, 0, 1, 0, 0, 0, 0, 1, 0],
}
df = pd.DataFrame(data)
df.groupby(['groupby_col'], as_index=False).rolling(4).agg({'agg_col': 'mean'})
Expected Output
pd.DataFrame({'agg_col': {
0: np.nan,
1: np.nan,
2: np.nan,
3: 0.75,
4: 0.5,
5: np.nan,
6: np.nan,
7: np.nan,
8: 0.25,
9: 0.25}})
agg_col | |
---|---|
0 | NaN |
1 | NaN |
2 | NaN |
3 | 0.75 |
4 | 0.50 |
5 | NaN |
6 | NaN |
7 | NaN |
8 | 0.25 |
9 | 0.25 |
Actual Output
pd.DataFrame({'agg_col': {
(0, 0): np.nan,
(0, 1): np.nan,
(0, 2): np.nan,
(0, 3): 0.75,
(0, 4): 0.5,
(1, 5): np.nan,
(1, 6): np.nan,
(1, 7): np.nan,
(1, 8): 0.25,
(1, 9): 0.25}})
agg_col | ||
---|---|---|
0 | 0 | NaN |
1 | NaN | |
2 | NaN | |
3 | 0.75 | |
4 | 0.50 | |
1 | 5 | NaN |
6 | NaN | |
7 | NaN | |
8 | 0.25 | |
9 | 0.25 |
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 0.25.1
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 40.8.0
Cython : 0.29.14
pytest : 5.3.2
hypothesis : None
sphinx : 2.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
IPython : 7.10.2
pandas_datareader: None
bs4 : 4.8.1
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.2
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.15.1
pytables : None
s3fs : None
scipy : 1.3.0
sqlalchemy : 1.3.11
tables : 3.6.1
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None