Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'ok.': 0.04; 'classes.': 0.05; 'suppose': 0.05; 'defines': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'substitute': 0.09; 'def': 0.13; '@property': 0.16; 'columns': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'series.': 0.16; 'subject:package': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'subject:problem': 0.19; 'from:addr:web.de': 0.23; 'classes': 0.26; 'import': 0.27; 'work:': 0.29; 'class': 0.29; 'skip:p 30': 0.29; 'clear,': 0.30; 'source': 0.32; 'named': 0.33; 'header:X-Complaints-To:1': 0.34; 'frame': 0.34; 'to:addr :python-list': 0.35; '...': 0.35; 'extend': 0.36; 'two': 0.36; 'received:org': 0.36; 'class.': 0.37; 'skip:_ 10': 0.38; 'some': 0.38; 'data': 0.38; 'returned': 0.39; 'might': 0.40; 'to:addr:python.org': 0.40; 'follow': 0.61; 'quick': 0.61; 'more': 0.61; 'your': 0.61; 'skip:n 30': 0.64; 'series': 0.67; 'fabrizio': 0.84; 'matrix': 0.84; 'reveals': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: package extension problem Date: Mon, 13 Feb 2012 19:28:05 +0100 Organization: None References: <19027093.1215.1329153496662.JavaMail.geo-discussion-forums@ynel5> <4F394E17.9090108@inrim.it> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b4de.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329157703 news.xs4all.nl 6887 [2001:888:2000:d::a6]:39751 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20357 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