[Scipy-svn] r2961 - in trunk/Lib/sandbox/timeseries: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Fri May 4 14:00:32 EDT 2007


Author: pierregm
Date: 2007-05-04 13:00:28 -0500 (Fri, 04 May 2007)
New Revision: 2961

Modified:
   trunk/Lib/sandbox/timeseries/tdates.py
   trunk/Lib/sandbox/timeseries/tests/test_timeseries.py
   trunk/Lib/sandbox/timeseries/tseries.py
Log:
tdates : added a _unsorted attributes, that keeps track of the initial order of the dates
tseries: ensures that the data are sorted along the dates

Modified: trunk/Lib/sandbox/timeseries/tdates.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tdates.py	2007-05-04 15:43:31 UTC (rev 2960)
+++ trunk/Lib/sandbox/timeseries/tdates.py	2007-05-04 18:00:28 UTC (rev 2961)
@@ -195,6 +195,7 @@
             _dates.shape = (1,)
         _dates = _dates.view(cls)
         _dates.freq = _freq
+        _dates._unsorted = None
         return _dates
 
     def __array_wrap__(self, obj, context=None):
@@ -205,6 +206,7 @@
 
     def __array_finalize__(self, obj):
         self.freq = getattr(obj, 'freq', _c.FR_UND)
+        self._unsorted = getattr(obj,'_unsorted',None)
         self._cachedinfo = dict(toobj=None, tostr=None, toord=None,
                                 steps=None, full=None, hasdups=None)
         if hasattr(obj,'_cachedinfo'):
@@ -487,7 +489,8 @@
 def _listparser(dlist, freq=None):
     "Constructs a DateArray from a list."
     dlist = numeric.asarray(dlist)
-    dlist.sort()
+    idx = dlist.argsort()
+    dlist = dlist[idx]
     if dlist.ndim == 0:
         dlist.shape = (1,)
     # Case #1: dates as strings .................
@@ -531,6 +534,7 @@
             dates = [Date(freq, datetime=dt.datetime.fromordinal(a)) for a in ords]
     #
     result = DateArray(dates, freq)
+    result._unsorted = idx
     return result
 
 
@@ -695,4 +699,4 @@
     if 1:
         "Tests the automatic sorting of dates."
         D = date_array_fromlist(dlist=['2006-01','2005-01','2004-01'],freq='M')
-        assert_equal(D.view(N.ndarray), [24037, 24049, 24061])
\ No newline at end of file
+        assert_equal(D.view(ndarray), [24037, 24049, 24061])
\ No newline at end of file

Modified: trunk/Lib/sandbox/timeseries/tests/test_timeseries.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tests/test_timeseries.py	2007-05-04 15:43:31 UTC (rev 2960)
+++ trunk/Lib/sandbox/timeseries/tests/test_timeseries.py	2007-05-04 18:00:28 UTC (rev 2961)
@@ -73,6 +73,7 @@
 
 
     def test_fromdatearray(self):
+        "Tests the creation of a series from a datearray"
         _, dates, _ = self.d
         data = dates
 
@@ -91,10 +92,28 @@
 
 
     def test_datafromlist(self):
+        "Check the creation of a time series from a list of data."
         (_, dates, _) = self.d
         data = list(range(15))
         series = time_series(data, dates)
         assert_equal(series._data.size, 15)
+        
+    def test_unsorted(self):
+        "Tests that the data are porperly sorted along the dates."
+        dlist = ['2007-01-%02i' % i for i in (3,2,1)]
+        data = [10,20,30]
+        series = time_series(data,dlist)
+        assert_equal(series._data,[30,20,10])
+        #
+        series = TimeSeries(data, dlist)
+        assert_equal(series._data,[30,20,10])
+        #
+        series = TimeSeries(data, dlist, mask=[1,0,0])
+        assert_equal(series._mask,[0,0,1])
+        #
+        data = masked_array([10,20,30],mask=[1,0,0])
+        series = TimeSeries(data, dlist)
+        assert_equal(series._mask,[0,0,1])
 #...............................................................................
 
 class test_arithmetics(NumpyTestCase):
@@ -463,7 +482,7 @@
         assert_equal(empty_ts.end_date, None)
 
     def test__timeseriescompat_multiple(self):
-
+        "Tests the compatibility of multiple time series."
         seriesM_10 = time_series(N.arange(10),
                                     date_array(
                                       start_date=Date(freq='m', year=2005, month=1),

Modified: trunk/Lib/sandbox/timeseries/tseries.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tseries.py	2007-05-04 15:43:31 UTC (rev 2960)
+++ trunk/Lib/sandbox/timeseries/tseries.py	2007-05-04 18:00:28 UTC (rev 2961)
@@ -324,11 +324,9 @@
         maparms = dict(copy=copy, dtype=dtype, fill_value=fill_value,
                        keep_mask=keep_mask, small_mask=small_mask,
                        hard_mask=hard_mask,)
-        # Get the data ...............................
-        _data = MaskedArray(data, mask=mask, **maparms).view(cls)
+        _data = MaskedArray(data, mask=mask, **maparms)
         # Get the frequency ..........................
         freq = check_freq(freq)
-
         # Get the dates ..............................
         if dates is None:
             newdates = getattr(data, '_dates', None)
@@ -350,7 +348,10 @@
                 newdates = date_array([], freq=freq)
         # Get observed ...............................
         observed = getattr(data, 'observed', fmtObserv(observed))
-
+        # Get the data ...............................
+        if newdates._unsorted is not None:
+            _data = _data[newdates._unsorted]
+        _data = _data.view(cls)
         if _data is masked:
             assert(numeric.size(newdates)==1)
             return _data.view(cls)
@@ -373,6 +374,7 @@
         return result
     #............................................
     def _get_series(self):
+        "Returns the series as a regular masked array."
         if self._mask.ndim == 0 and self._mask:
             return masked
         return self.view(MaskedArray)
@@ -945,10 +947,16 @@
             dates = date_array([], freq=freq)
     elif not isinstance(dates, DateArray):
         dates = date_array(dlist=dates, freq=freq)
-    return TimeSeries(data=data, dates=dates, mask=mask, observed=observed,
-                      copy=copy, dtype=dtype, fill_value=fill_value,
-                      keep_mask=keep_mask, small_mask=small_mask,
-                      hard_mask=hard_mask,)
+    if dates._unsorted is not None:
+        idx = dates._unsorted
+        data = data[idx]
+        if mask is not nomask:
+            mask = mask[idx]
+        dates._unsorted = None
+    return TimeSeries(data=data, dates=dates, mask=mask, 
+                      observed=observed, copy=copy, dtype=dtype, 
+                      fill_value=fill_value, keep_mask=keep_mask, 
+                      small_mask=small_mask, hard_mask=hard_mask,)
 
 
 def isTimeSeries(series):
@@ -1452,6 +1460,7 @@
     return newseries
 #...............................................................................
 def empty_like(series):
+    """Returns an empty series with the same dtype, mask and dates as series."""
     result = N.empty_like(series).view(type(series))
     result._dates = series._dates
     result._mask = series._mask




More information about the Scipy-svn mailing list