BUG: groupby iter on pandas 1.1.x not propagating _metadata on DataFrame subclasses · Issue #37343 · pandas-dev/pandas (original) (raw)


Code Sample, a copy-pastable example

In [1]: import pandas as pd ...: ...: class SubclassedDataFrame2(pd.DataFrame): ...: ...: # temporary properties ...: _internal_names = pd.DataFrame._internal_names + ['internal_cache'] ...: _internal_names_set = set(_internal_names) ...: ...: # normal properties ...: _metadata = ['added_property'] ...: ...: @property ...: def _constructor(self): ...: return SubclassedDataFrame2 ...:

In [2]: df = SubclassedDataFrame2({'A': [1, 2, 3], 'B': [1, 1, 2], 'C': [7, 8, 9]}) ...: df.added_property = "hello" ...: for i, d in df.groupby("B"): ...: assert d.added_property == "hello" ...:

AttributeError Traceback (most recent call last) in 2 df.added_property = "hello" 3 for i, d in df.groupby("B"): ----> 4 assert d.added_property == "hello" 5

~\RMS-Python\lib\site-packages\pandas\core\generic.py in getattr(self, name) 5133 or name in self._accessors 5134 ): -> 5135 return object.getattribute(self, name) 5136 else: 5137 if self._info_axis._can_hold_identifiers_and_holds_name(name):

AttributeError: 'SubclassedDataFrame2' object has no attribute 'added_property'

Problem description

Under pandas 1.0.x the above is working, on 1.1.x, the _metadata is not propagated to the split frames. I think this is a known issue with groupby and __finalize__, but this specific behavior with __iter__ is a regression from 1.0.x behavior on DataFrame subclasses.

Expected Output

Assert statements above are passing, with metadata propagated to the split dataframe

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.7.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.1.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : 0.29.17
pytest : 5.4.1
hypothesis : 5.36.1
sphinx : 2.4.4
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : 1.3.2
fsspec : 0.8.0
fastparquet : None
gcsfs : None
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.49.0