Skip to content

_metadata items of subclassed pd.Series are not propagated into corresponding SubclassedDataFrame #32860

Open
@johannes-mueller

Description

@johannes-mueller

Code Sample

import pandas as pd

# Define a subclass for pd.Series with metadata myprop
class SubclassedSeries(pd.Series):

    _metadata = ['myprop']

    @property
    def _constructor(self):
        return SubclassedSeries

    @property
    def _constructor_expanddim(self):
        return SubclassedDataFrame

# Define a subclass for pd.DataFrame that slices to SubclassedSeries
class SubclassedDataFrame(pd.DataFrame):
    @property
    def _constructor(self):
        return SubclassedDataFrame

    @property
    def _constructor_sliced(self):
        return SubclassedSeries

# make an instance of SubclassedSeries and set myprop
sr = SubclassedSeries([1,2,3])
sr.myprop = 'foo'
print("myprob is", sr.myprop) # Works

# put the SubclassedSeries object into a SubclassedDataFrame and try to get myprop
df = SubclassedDataFrame({'a': sr})
print(type(df.a)) # Works (prints <class '__main__.SubclassedSeries'>)
print("myprob is", df.a.myprop) # does not work (AttributeError: 'SubclassedSeries' object has no attribute 'myprop')

Problem description

_metadata items of pd.Series subclasses are not propagated when the SubclassedSeries object is put into a SubclassedDataFrame. I would expect myprop to be available in the new SubclassedDataFrame.

Expected Output

myprob is foo
<class '__main__.SubclassedSeries'>
myprob is foo

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.8.1.final.0 python-bits : 64 OS : Linux OS-release : 5.3.0-42-lowlatency machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.UTF-8

pandas : 1.0.2
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0.post20200309
Cython : 0.29.15
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : 0.15.0
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None

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