0

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.

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.