[Python] How can I subclass a Pandas DataFrame?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    Subclassing Pandas classes seems a common need, but I could not find references on the subject. (It seems that Pandas developers are still working on it: Easier subclassing #60.)

    There are some SO questions on the subject, but I am hoping that someone here can provide a more systematic account on the current best way to subclass pandas.DataFrame that satisfies two general requirements:

    1. calling standard DataFrame methods on instances of MyDF should produce instances of MyDF
    2. calling standard DataFrame methods on instances of MyDF should leave all attributes still attached to the output

    (And are there any significant differences for subclassing pandas.Series?)

    Code for subclassing pd.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.
    mydf.myattr = 1
    mydf_cp1 = MyDF(mydf)
    mydf_cp2 = mydf.copy()
    print(hasattr(mydf_cp1, 'myattr')) # False
    print(hasattr(mydf_cp2, 'myattr')) # False

    Continue reading...

Compartilhe esta Página