简体   繁体   中英

Add columns to extended `pandas.DataFrame` class

I created a subclass of pandas.DataFrame to bundle a few functionalities:

class ABT(pandas.DataFrame):
    def __init__(self, data=None, ...):
        if data is None:
            ...
            data = DataFrame(..., tz='utc'))
        super(ABT, self).__init__(data)

I want to make a method that uses another DataFrame as a parameter and appends it to ABT . The question is: How to join/merge/concat to self ?

    def add_df(self, new_df):
        df_utc = new_df.tz_localize('CET', ambiguous='NaT').tz_convert('utc')

        ...

        self.merge(df_utc , how='left', inplace=True)

The above method does not work, but I hope there is a ncie way to solve this.

There is no inplace keyword for merge . Besides of this, you should read Subclassing pandas datastructures . So you need something like this:

class ABT(pd.DataFrame):

    @property
    def _constructor(self):
        return ABT

    @property
    def _constructor_sliced(self):
        return pd.Series

    def add_df(self, new_df):
        return self.merge(new_df, how='left')



abt1 = ABT(df1)
abt2 = ABT(df2)
abt1 = abt1.add_df(abt2)

If you don't define the constructors, the result of add_df() will be of type pandas.core.frame.DataFrame instead of ABT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM