Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20357

Re: package extension problem

From Peter Otten <__peter__@web.de>
Subject Re: package extension problem
Date 2012-02-13 19:28 +0100
Organization None
References <19027093.1215.1329153496662.JavaMail.geo-discussion-forums@ynel5> <4F394E17.9090108@inrim.it>
Newsgroups comp.lang.python
Message-ID <mailman.5769.1329157703.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

package extension problem Fabrizio Pollastri <f.pollastri@inrim.it> - 2012-02-12 19:46 +0100
  Re: package extension problem Miki Tebeka <miki.tebeka@gmail.com> - 2012-02-13 09:18 -0800
    Re: package extension problem Fabrizio Pollastri <f.pollastri@inrim.it> - 2012-02-13 18:53 +0100
      Re: package extension problem Miki Tebeka <miki.tebeka@gmail.com> - 2012-02-13 09:58 -0800
        Re: package extension problem Terry Reedy <tjreedy@udel.edu> - 2012-02-13 14:12 -0500
      Re: package extension problem Miki Tebeka <miki.tebeka@gmail.com> - 2012-02-13 09:58 -0800
    Re: package extension problem Peter Otten <__peter__@web.de> - 2012-02-13 19:28 +0100
  Re: package extension problem Miki Tebeka <miki.tebeka@gmail.com> - 2012-02-13 09:18 -0800

csiph-web