[Pandas-dev] Pandas astype() changes the class type
Simeon Simeonov
simeon.simeonov.s at gmail.com
Mon Dec 13 07:14:35 EST 2021
Hi all,
I saw this behaviour and I don't know if this is a bug or feature. I don't
have much experience with directly inheriting from pandas.DataFrame as I've
always preferred aggregation rather than inheritance there. A working
sample is pasted below. Notice how *df.astype(dtypes)* changes the type to
pandas.DataFrame. Any suggestions if this is intended behaviour?
import pandas as pd
class DF(pd.DataFrame): @property
def _constructor(self):
return self.__class__
df = DF({
'A': [1,2,3],
'B': [10,20,30],
'C': [100,200,300],
}) # Type is DF
a = df['A'] # type is Series
ab = df[['A', 'B']] # type is DF
dtypes = {'A': 'float64', 'B': 'float64', 'C': 'float64'}
x = df.astype(dtypes)
type(x) # type is pd.DataFrame
Regards,
Simeon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/pandas-dev/attachments/20211213/e8690b79/attachment.html>
More information about the Pandas-dev
mailing list