[Scipy-svn] r2629 - trunk/Lib/sandbox/timeseries

scipy-svn at scipy.org scipy-svn at scipy.org
Mon Jan 29 16:01:03 EST 2007


Author: mattknox_ca
Date: 2007-01-29 15:00:59 -0600 (Mon, 29 Jan 2007)
New Revision: 2629

Modified:
   trunk/Lib/sandbox/timeseries/tseries.py
Log:
added tshift function and method. Removed redundant definition of convert as a method in the TimeSeries class

Modified: trunk/Lib/sandbox/timeseries/tseries.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tseries.py	2007-01-29 15:19:29 UTC (rev 2628)
+++ trunk/Lib/sandbox/timeseries/tseries.py	2007-01-29 21:00:59 UTC (rev 2629)
@@ -517,11 +517,8 @@
         if freq is None:
             return self
         return TimeSeries(self._series, dates=self._dates.asfreq(freq))
-    
-    def convert(self, freq, func='auto', position='END'):
-        "Converts the dates to another frequency, and adapt the data."
-        return convert(self, freq, func=func, position=position)
         
+        
 ##### --------------------------------------------------------------------------
 ##--- ... Additional methods ...
 ##### --------------------------------------------------------------------------
@@ -1034,8 +1031,12 @@
     newshape = tuple(newshape)
 
     newdata = masked_array(numeric.empty(newshape, dtype=a.dtype), mask=True)
-    newseries = TimeSeries(newdata, newdates)
     
+    # need to ensure that we preserve attributes of the series in the result
+    options = dict(fill_value=a.fill_value, observed=a.observed)
+    
+    newseries = TimeSeries(newdata, newdates, **options)
+    
     if dstart is not None:
         start_date = max(start_date, dstart)
         end_date = min(end_date, dend) + 1
@@ -1136,9 +1137,46 @@
                            observed=series.observed, 
                            start_date=start_date)
     return newseries
+#....................................................................
+def tshift(series, nper):
+    """Returns a series of the same size as `series`, with the same
+start_date and end_date, but values shifted by `nper`. This is useful
+for doing things like calculating a percentage change.
+Eg. pct_change = 100 * (series/tshift(series, -1) - 1)
 
+:Example:
+>>> series = tseries.time_series([0,1,2,3], start_date=tdates.Date(freq='A', year=2005))
+>>> series
+timeseries(data  = [0 1 2 3],
+           dates = [2005 ... 2008],
+           freq  = A)
+>>> tshift(series, -1)
+timeseries(data  = [-- 0 1 2],
+           dates = [2005 ... 2008],
+           freq  = A)
+"""
+    newdata = masked_array(numeric.empty(series.shape, dtype=series.dtype), mask=True)
 
+    # need to ensure that we preserve attributes of the series in the result
+    options = dict(fill_value=series.fill_value, observed=series.observed)
+    newseries = TimeSeries(newdata, series._dates, **options)
+
+    if nper < 0:
+        nper = max(-series.shape[0],nper)
+        newseries[-nper:] = series._series[:nper].copy()
+        newseries[:-nper] = masked
+    elif nper > 0:
+        nper = min(series.shape[0],nper)
+        newseries[-nper:] = masked
+        newseries[:-nper] = series._series[nper:].copy()
+    else:
+        newseries[:] = self._series[:].copy()
+
+    return newseries
+#....................................................................
 TimeSeries.convert = convert
+TimeSeries.tshift = tshift
+
 #....................................................................
 def fill_missing_dates(data, dates=None, freq=None,fill_value=None):
     """Finds and fills the missing dates in a time series.




More information about the Scipy-svn mailing list