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

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Jan 30 10:48:06 EST 2007


Author: mattknox_ca
Date: 2007-01-30 09:48:00 -0600 (Tue, 30 Jan 2007)
New Revision: 2639

Modified:
   trunk/Lib/sandbox/timeseries/tseries.py
Log:
added copy flag to tshift function (True by default)

Modified: trunk/Lib/sandbox/timeseries/tseries.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tseries.py	2007-01-30 14:29:09 UTC (rev 2638)
+++ trunk/Lib/sandbox/timeseries/tseries.py	2007-01-30 15:48:00 UTC (rev 2639)
@@ -1152,12 +1152,22 @@
 TimeSeries.convert = convert
 
 #...............................................................................
-def tshift(series, nper):
+def tshift(series, nper, copy=True):
     """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)
+Eg. pct_change = 100 * (series/tshift(series, -1, copy=False) - 1)
+Note: By default the data is copied, but if you are using the result in
+a way that is going to make a copy anyway (like the above example) then
+you may want to bypass copying the data.
 
+:Parameters:
+    - `series` (TimeSeries) : TimeSeries object to shift
+    - `nper` (int) : number of periods to shift. Negative numbers
+      shift values to the right, positive to the left
+    - `copy` (boolean, *[True]*) : copies the data if True, returns
+      a view if False.
+
 :Example:
 >>> series = tseries.time_series([0,1,2,3], start_date=tdates.Date(freq='A', year=2005))
 >>> series
@@ -1173,7 +1183,11 @@
     options = dict(fill_value=series.fill_value, observed=series.observed)
     newdata = masked_array(numeric.empty(series.shape, dtype=series.dtype), 
                            mask=True)
-    inidata = series._series.copy()
+    if copy:
+        inidata = series._series.copy()
+    else:
+        inidata = series._series
+    
     if nper < 0:
         nper = max(-len(series), nper)
         newdata[-nper:] = inidata[:nper]




More information about the Scipy-svn mailing list