[Scipy-svn] r2443 - trunk/Lib/sandbox/timeseries/examples

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Dec 20 12:11:57 EST 2006


Author: mattknox_ca
Date: 2006-12-20 11:11:54 -0600 (Wed, 20 Dec 2006)
New Revision: 2443

Modified:
   trunk/Lib/sandbox/timeseries/examples/example.py
Log:
updated examples to work with latest version of code

Modified: trunk/Lib/sandbox/timeseries/examples/example.py
===================================================================
--- trunk/Lib/sandbox/timeseries/examples/example.py	2006-12-20 17:11:26 UTC (rev 2442)
+++ trunk/Lib/sandbox/timeseries/examples/example.py	2006-12-20 17:11:54 UTC (rev 2443)
@@ -4,7 +4,7 @@
 
  
 # create a time series at business frequency and fill it with random data
-bSer = ts.TimeSeries(np.random.uniform(-100,100,600),dtype=np.float64,freq='B',observed='SUMMED',startIndex=ts.thisday('B')-600)
+bSer = ts.TimeSeries(np.random.uniform(-100,100,600),dtype=np.float64,freq='B',observed='SUMMED',start_date=ts.thisday('B')-600)
 
 
 # Set negative values to zero.
@@ -12,7 +12,7 @@
 
 
 # Set values occurring on Fridays to 100.
-weekdays = ts.day_of_week(ts.tser(bSer.firstValue(asDate=True),bSer.lastValue(asDate=True)))
+weekdays = ts.day_of_week(ts.tser(bSer.start_date(),bSer.end_date()))
 bSer[weekdays == 4] = 100
 
 
@@ -23,7 +23,7 @@
 function that acts on a 1-dimension masked array and returns a single
 value.
 """
-mSer1 = bSer.convert('M',func=ma.average)
+mSer1 = bSer.convert('M',func=ts.average)
 
 
 """
@@ -53,45 +53,45 @@
 
 
 # create another monthly frequency series
-mSer2 = ts.TimeSeries(np.random.uniform(-100,100,100),dtype=np.float64,freq='m',observed='END',startIndex=ts.thisday('M')-110)
+mSer2 = ts.TimeSeries(np.random.uniform(-100,100,100),dtype=np.float64,freq='m',observed='END',start_date=ts.thisday('M')-110)
 
 
 """
 Slicing also supported. The intention is to have indexing behave
-largely in the same manner as regular numpy arrays. It sure would be
-nice if we could slice with the dates directly, but as it stands we
-shall have to cast the dates to integers
+largely in the same manner as regular numpy arrays.
+
+series.adjust_date  convert a date object into the corresponding
+integer for indexing the series
 """
-mSer2[int(ts.thisday('m')-60):int(ts.thisday('m')-45)] = 12
+sixtyMonthsAgoIdx = mSer2.date_to_index(ts.thisday('m')-60)
+mSer2[sixtyMonthsAgoIdx:sixtyMonthsAgoIdx+10] = 12
 
 
-# Mask a value. series.lastValue() returns the index of the last
-# unmasked value in the series (as an integer, not a Date object)
-mSer2[mSer2.lastValue()-40] = ts.masked #ts.masked is the same thing as numpy.ma.masked
+# Mask the last value in the series
+mSer2[-1] = ts.masked #ts.masked is the same thing as numpy.ma.masked
 
 
 """
-Only series of the same frequency can be used in the basic operations.
+Only series of the same frequency and size and same start date
+can be used in the basic operations.
+
 The results are the same as you would expect for masked arrays with the
 basic operations.
 
-Notice that the start and end indices of mSer1 and mSer2 do not need to
-line up. This conversion is done implicitly.    
+start_date and end_date are optional parameters to the aligned function.
+If omitted, the min start_date() and end_date() of all series is used as
+the new boundaries for each series.
 """
+mSer1, mSer2 = ts.aligned(mSer1, mSer2, start_date=ts.thisday('m')-100, end_date=ts.thisday('m'))
 mAdd1 = mSer1 + mSer2
 
 
-"""
-if you want more control over behaviour of masked values, use ts.add
-(or multiply, etc) instead.
+# add the two series together, first filling in masked values with zeros
+mAdd1_filled = mSer1.filled(fill_value=0, ts=True) + mSer2.filled(fill_value=0, ts=True)
 
-if a fill_value is specified, both TimeSeries objects are filled from
-min(mSer1.firstValue(),mSer2.firstValue()) to max(mSer1.lastValue(),mSer2.lastValue())
-wherever the series are masked before performing the operation
-"""
-mAdd2 = ts.add(mSer1, mSer2, fill_value=0)
+# adjust the start and end dates of a series
+newSer = ts.adjust_endpoints(mSer1, start_date=ts.Date(freq='M', year=1954, month=5),  end_date=ts.Date(freq='M', year=2000, month=6))
 
-
 # calculate the average value in the series. Behaves the same as in ma
 bAverage = ts.average(bSer)
 




More information about the Scipy-svn mailing list