package extension problem

Peter Otten __peter__ at web.de
Mon Feb 13 13:28:05 EST 2012


Fabrizio Pollastri wrote:

> Ok. To be more clear, consider the real python package Pandas.
> 
> This package defines a Series class and a DataFrame class.
> The DataFrame is a matrix that can have columns of
> different type.
> 
> If I write
> 
> import pandas as pd
> df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
> 
> a data frame with two cols named A and B is created.
> 
> If I write
> 
> col_A = df['A']
> 
> the returned col_A is an instance of Series.
> 
> Now , let suppose that I want to extend some functionality of pandas
> by adding new methods to both Series and DataFrame classes.
> 
> One way to do this is to redefine this classes in a new package
> (new_pandas) as follow
> 
> import pandas as pd
> 
> class Series(pd.Series):
>       ...
>       add new methods
>       ...
> 
> class DataFrame(pd.DataFrame):
>       ...
>       add new methods
>       ...
> 
> When I use the new package as a pandas substitute and write
> 
> import new_pandas as np
> df = np.DataFrame({'A':[1,2,3],'B':[4,5,6]})
> col_A = df['A']
> 
> col_A is an instance of the original pandas and not of the new pandas,
> losing all the added functionality.

A quick look into the pandas source reveals that the following might work:

# untested
class DataFrame(pd.DataFrame):
    @property
    def _constructor(self):
        return DataFrame # your class
    # your new methods





More information about the Python-list mailing list