Skip to content

BUG: groupby().ffill() strips level names from MultiIndex on columns #41564

Closed
@DomKennedy

Description

@DomKennedy
  • 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.


Code Sample, a copy-pastable example

import pandas as pd

df = pd.DataFrame(
    [[1,2,3,4],[5,6,7,8]],
    columns=pd.MultiIndex.from_product(
        [[0,1], ['foo','bar']], 
        names=['x', 'y']
    )
)

after_groupby = df.groupby(df.index).ffill()

print(f"original dataframe:\n{df}\n")
print(f"result of groupby().ffill():\n{after_groupby}\n")
print(f"original dataframe columns.names:\n{df.columns.names}\n")
print(f"columns.names after groupby().ffill():\n{after_groupby.columns.names}\n")

Actual output

original dataframe:
x   0       1
y foo bar foo bar
0   1   2   3   4
1   5   6   7   8

result of groupby().ffill():
    0       1
  foo bar foo bar
0   1   2   3   4
1   5   6   7   8

original dataframe columns.names:
['x', 'y']

columns.names after groupby().ffill():
[None, None]

Expected output

original dataframe:
x   0       1
y foo bar foo bar
0   1   2   3   4
1   5   6   7   8

result of groupby().ffill():
x   0       1
y foo bar foo bar
0   1   2   3   4
1   5   6   7   8

original dataframe columns.names:
['x', 'y']

columns.names after groupby().ffill():
['x', 'y']

Problem description

Calling groupby().ffill() on a DataFrame with a MultiIndex column index strips the names attribute of the column index. The above shows the trivial case of groupby(df.index), but I have also found it to occur with nontrivial by arguments (e.g. grouping a DatetimeIndex by dates).

This is obviously unexpected and undesirable.

Output of pd.show_versions()

I have observed this bug on WSL with Python 3.6 and pandas 1.1.5, as well as an M1 MacBook Air with the latest available M1-native pandas version from conda. The details of the latter are below.

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.2.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.0
IPython : 7.23.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions