子类化pandas类似乎是一个常见的需求,但我找不到关于这个主题的参考.(似乎熊猫开发者仍在努力:https://github.com/pydata/pandas/issues/60).
关于这个主题有一些SO主题,但我希望这里有人可以提供一个更系统的帐户,目前最好的方法是将pandas.DataFrame子类化,满足两个,我认为,一般要求:
import numpy as np
import pandas as pd
class MyDF(pd.DataFrame):
# how to subclass pandas DataFrame?
pass
mydf = MyDF(np.random.randn(3,4), columns=['A','B','C','D'])
print type(mydf) # <class '__main__.MyDF'>
# Requirement 1: Instances of MyDF, when calling standard methods of DataFrame,
# should produce instances of MyDF.
mydf_sub = mydf[['A','C']]
print type(mydf_sub) # <class 'pandas.core.frame.DataFrame'>
# Requirement 2: Attributes attached to instances of MyDF, when calling standard
# methods of DataFrame, should still attach to the output. …Run Code Online (Sandbox Code Playgroud)