简体   繁体   中英

Creating a pandas.DataFrame wrapper that has a method to return a dataframe

I'm trying to create a class that is a wrapper around pandas.DataFrame objects. After I write my own methods for this class, I would like pandas' methods to be also available, but in a way that explicitly tells me/the user that their are from pandas. It would work like this

df = pd.DataFrame(np.random.randn(5,2))
md = myData(df)

a = md.df # returns the original pandas.DataFrame "df" to a (equivalent of a=df
print(md) # prints as myData class
print(md.df) # prints just as print(df) would. Equiv to print(df)

md.mean() # mean as defined in myData class. Returns myData object
md.df.mean() # mean as defined in pandas. Returns dataframe object

md.std() # myData std
md.df.std() # pandas std

So far all I was able to do were unsuccessful attempts. One thing that I really thought it should but doesn't is

import pandas as _pd
class myData(_pd.DataFrame):
    """
    Attempt to create a myData object
    """
    def __init__(self, df, dic):
        df = df.copy()
        print(type(df))
        self.df = df
        self = df

It exits with RuntimeError: maximum recursion depth exceeded while calling a Python object .

EDIT

The following code ends with the same error.

import pandas as _pd
class myData(_pd.DataFrame):
    """
    Attempt to create a myData object
    """
    def __init__(self, df, dic):
        df = df.copy()
        self.dic = dic
        super(myData, self).__init__(df)
        self.df = df

However, if I try

    def __init__(self, df, dic):
        df = df.copy()
        super(myData, self).__init__(df)

Then it works, but the result is a myData object which is really a DataFrame, since every method is already that of DataFrames.

Any idea of what might be wrong with the code or if there is a way to make this better?

You can not use DataFrame as the parent:

class myData(object):
    """
    Attempt to create a myData object
    """
    def __init__(self, df):
        self.df = df.copy()

df = pd.DataFrame(np.random.randn(100, 5), columns=list('ABCDE'))
mdf = myData(df)
mdf.df.describe()

在此输入图像描述

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