Skip to content

BUG: _metadata does not persist through groupby #38269

@stuart23

Description

@stuart23
  • 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

class MyDataFrame(DataFrame):
    _metadata = ['my_metadata']

data = MyDataFrame({'test': [1,1,1,2,2,2]})
data.my_metadata = 2
for group_id, group_data in data.groupby('test'):
    assert group_data.my_metadata == 2

Problem description

As per documentation here, it seems that any derivatives of a DataFrame should inherit the metadata properties assigned. This is true with slicing operations, but is not true with groupby(). It would be more consistent if the property persisted for slicing and groupby().

Expected Output

Should not raise an assertion error.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-56-generic
Version : #62-Ubuntu SMP Mon Nov 23 19:20:19 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.4
numpy : 1.19.4
pytz : 2019.3
dateutil : 2.7.3
pip : 20.0.2
setuptools : 45.2.0
Cython : 0.29.21
pytest : 6.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
sqlalchemy : None
tables : None
tabulate : 0.8.6
xarray : 0.14.1
xlrd : None
xlwt : None
numba : 0.51.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions