[SciPy-user] Subclass of TimeSeries

Pierre GM pgmdevlist@gmail....
Wed May 20 18:50:58 CDT 2009


On May 20, 2009, at 7:12 PM, Robert Ferrell wrote:

> How do I derive a subclass from TimeSeries?  I tried
>
>
> import scikits.timeseries as ts
> import numpy as np
>
> class MyTS(ts.TimeSeries):
> 	def __init__(self, label, dataArray, dateArray):
> 		ts.TimeSeries.__init__(self, data=dataArray, dates=dateArray)
> 		self.label=label
> 		return

Just like the use of __init__ is not the way to subclass ndarray  
(except when subclassing also from an object that requires __init__),  
that's not the way to go. You need to implement a __new__ and a  
__array_finalize__ as described here:
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

The easiest is to follow some of the examples given in the  
scikits.hydroclimpy package (http://hydroclimpy.sourceforge.net/).
For instance, here's a class that attaches a reference period to the  
series (in scikits/hydroclimpy/core/base.py). Adapting this example to  
your case should be straightforward. Nevertheless, don't hesitate to  
ask for more details/info as needed.

Cheers
P.



###

class ReferencedSeries(TimeSeries, object):

     def __new__(cls, data, dates=None, mask=nomask, refperiod=None,
                 freq=None, start_date=None, autosort=True,
                 dtype=None, copy=False, **options):
         (maoptions, options) = get_maskoptions(**options)
         maoptions.update(dict(copy=copy, dtype=dtype))
         _data = ts.time_series(data, dates=dates, mask=mask, freq=freq,
                                start_date=start_date,  
autosort=autosort,
                                **maoptions).view(cls)
         # Set the reference period
         if refperiod is None or tuple(refperiod) == (None, None):
             # Don't call  refperiod yet, in case we come from a pickle
             _data._optinfo['reference_period'] = refperiod
         else:
             # OK, here we can call refperiod
             _data.refperiod = refperiod
         return _data
###




More information about the SciPy-user mailing list