[SciPy-user] Subclass of TimeSeries

Robert Ferrell ferrell@diablotech....
Wed May 20 22:56:16 CDT 2009

On May 20, 2009, at 5:50 PM, Pierre GM wrote:

> 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.

Thanks for the response.  I think I get the idea.  For my purposes, it  
seems to work if I just call the TimeSeries.__new__ method.  That way  
I don't have to get into maoptions.

I don't think I need to call __array_finalize__.  I think I can use  
__init__ for my case.  I hope I've understood the docs correctly on  
that.  I definitely did not understand that part completely.

thanks for the help,

Here's what seems to do what I want.


import scikits.timeseries as ts
import numpy as np
from numpy import ma
from numpy.ma import nomask

import unittest

class MyTS(ts.TimeSeries):
	def __new__(cls, label, data, dates, mask=nomask, dtype=None,
				copy=False, fill_value=None, subok=True, keep_mask=True,
				hard_mask=False, autosort=True, **options):
		cls = ts.TimeSeries.__new__(cls, data=data, dates=dates, mask=mask,  
		copy=copy, fill_value=fill_value, subok=subok, keep_mask=keep_mask,
		hard_mask=hard_mask, autosort=autosort, **options)
		return cls
	def __init__(self, label, **args):

class MyTSTests(unittest.TestCase):
	def setUp(self):
		self.dateArray = ts.date_array(start_date = ts.Date('d',  
'2009-1-1'), length=10, freq='d')
		self.A = np.random.random(10)
		self.B = np.random.random(10)
		self.myDtype = [('a', np.float64), ('b', np.float64)]
		self.myData = np.array(zip(self.A,self.B), dtype=self.myDtype)
	def test_1_instantiate(self):
		"""Test that we can instantiate a MyTS instance."""
		myts = MyTS(label='doesWork', data=zip(self.A,self.B),  
dates=self.dateArray, dtype=self.myDtype)
		self.failUnless(isinstance(myts, MyTS), 'Expected an instance of %s,  
but got %s.' % (MyTS, type(myts)))
	def test_2_label(self):
		"""Test that the label got set"""
		myts = MyTS(label='doesWork', data=zip(self.A,self.B),  
dates=self.dateArray, dtype=self.myDtype)
		self.failUnless(myts.label == 'doesWork', 'Expected label %s, but  
got label %s.' % ('doesWork', myts.label))
	def test_3_field(self):
		"""Test that we can get the fields and they have the correct data."""
		myts = MyTS(label='doesWork', data=zip(self.A,self.B),  
dates=self.dateArray, dtype=self.myDtype)
		# Put the field accessor first
		self.failUnless((myts['a'].data == self.A).all())
		# Or apply it to the data
		self.failUnless((myts.data['b'] == self.B).all())

MyTSTestSuite = unittest.TestLoader().loadTestsFromTestCase(MyTSTests)

if __name__ == '__main__':

More information about the SciPy-user mailing list