BUG: for DataFrame subclass, astype with dict does not keep the subclass · Issue #40810 · pandas-dev/pandas (original) (raw)
- 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