Skip to content

BUG: for DataFrame subclass, astype with dict does not keep the subclass #40810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
mitar opened this issue Apr 6, 2021 · 1 comment · Fixed by #45272
Closed
2 tasks done

BUG: for DataFrame subclass, astype with dict does not keep the subclass #40810

mitar opened this issue Apr 6, 2021 · 1 comment · Fixed by #45272
Labels
Astype Bug Subclassing Subclassing pandas objects
Milestone

Comments

@mitar
Copy link
Contributor

mitar commented Apr 6, 2021

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.

Code Sample, a copy-pastable example

import numpy
import pandas

class CustomDataFrame(pandas.DataFrame):
    @property
    def _constructor(self) -> type:
        return CustomDataFrame

a = CustomDataFrame({'a': [1,2,3], 'b': ['A','B','C']})

print(type(a))

b = a.astype({'a': numpy.int64, 'b': object})

print(type(b))

Problem description

If I use astype on a DataFrame subclass, I would expect that the DataFrame class do not change, and only dtypes of columns change.

Expected Output

<class '__main__.CustomDataFrame'>
<class '__main__.CustomDataFrame'>

But I get:

<class '__main__.CustomDataFrame'>
<class 'pandas.core.frame.DataFrame'>

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-48-generic
Version : #54~20.04.1-Ubuntu SMP Sat Mar 20 13:40:25 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.3
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
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

@mitar mitar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 6, 2021
@mroeschke mroeschke added Subclassing Subclassing pandas objects and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 19, 2021
@EmilianoJordan
Copy link

EmilianoJordan commented Oct 24, 2021

This exact issue is caused in the above example by not defining a sub classed Series and the _constructor_sliced property on the CustomDataFrame. For example this code works as expected:

import numpy
import pandas as pd


class SubclassedSeries(pd.Series):
    @property
    def _constructor(self):
        return SubclassedSeries

    @property
    def _constructor_expanddim(self):
        return SubclassedDataFrame


class SubclassedDataFrame(pd.DataFrame):
    @property
    def _constructor(self):
        return SubclassedDataFrame

    @property
    def _constructor_sliced(self):
        return SubclassedSeries


a = SubclassedDataFrame({'a': [1,2,3], 'b': ['A','B','C']})

b = a.astype({'a': numpy.int64, 'b': object})

assert isinstance(b, SubclassedDataFrame)

This exact issue could be solved in NDFrame.astype but I imagine that a sub classed DataFrame and sub classed Series are intertwined in a lot more places than just astype. My suggestion, and I'd be happy to implement it, would be to note this behavior in the docs and that both sub classed types need to be implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astype Bug Subclassing Subclassing pandas objects
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants