При наследовании от pd.DataFrame мне непонятно, почему astype() меняет результат обратно на pd.DataFrame, а не на новый подкласс типа pd.DataFrame? Есть ли @property, который я могу определить аналогично _constructor, чтобы убедиться, что df.astype() возвращает правильный тип?
документы: https://pandas.pydata.org/docs/developm ... nding.html
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
Подробнее здесь: https://stackoverflow.com/questions/702 ... class-type
Расширение pandas: astype меняет тип класса DataFrame ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение