【发布时间】:2019-08-15 15:43:54
【问题描述】:
我有以下例子:
import pandas as pd
from copy import copy, deepcopy
class DataFrameWrapper(pd.DataFrame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __eq__(self, other):
return self.equals(other)
t1 = DataFrameWrapper(pd.DataFrame({'a': [1, 2, 3]}))
t2 = deepcopy(t1)
t3 = copy(t1)
print(type(t1), ' ', type(t2), ' ', type(t3))
输出:
<class 'DataFrameWrapper'> <class 'pandas.core.frame.DataFrame'> <class 'pandas.core.frame.DataFrame'>
有谁能告诉我为什么 copy 和 deepcopy 会修改 t1 的类型?
DataFrameWrapper 类的目的只是让我在 pandas DataFrames 之间做一个==。
【问题讨论】:
-
为什么要两次调用
DataFrame的构造函数? -
@OlvinRoght 你是对的。你可以做
t1 = DataFrameWrapper({'a': [1, 2, 3]})并得到我在我的问题中指出的同样的问题 -
@Taran 请检查我的回答,如果有帮助请点赞并接受。
标签: python pandas dataframe copy deep-copy