Changeset 1208
- Timestamp:
- 08/08/08 10:24:48 (4 months ago)
- Files:
-
- trunk/timeseries/scikits/timeseries/src/c_dates.c (modified) (1 diff)
- trunk/timeseries/scikits/timeseries/tdates.py (modified) (3 diffs)
- trunk/timeseries/scikits/timeseries/tseries.py (modified) (26 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/timeseries/scikits/timeseries/src/c_dates.c
r1163 r1208 1695 1695 static char DateObject_strftime_doc[] = 1696 1696 "Returns string representation of Date object according to format specified.\n\n" 1697 "*Parameters*:\n" 1698 " fmt : {str}\n" 1699 " Formatting string. Uses the same directives as in the time.strftime\n" 1700 " function in the standard Python time module. In addition, a few other\n" 1701 " directives are supported:\n" 1702 " %q - the 'quarter' of the date\n" 1703 " %f - Year without century as a decimal number [00,99]. The\n" 1704 " 'year' in this case is the year of the date determined by\n" 1705 " the year for the current quarter. This is the same as %y\n" 1706 " unless the Date is one of the 'qtr-s' frequencies\n" 1707 " %F - Year with century as a decimal number. The 'year' in this\n" 1708 " case is the year of the date determined by the year for\n" 1709 " the current quarter. This is the same as %Y unless the\n" 1710 " Date is one of the 'qtr-s' frequencies\n"; 1697 "Parameters\n" 1698 "----------\n" 1699 "fmt : {str}\n" 1700 " Formatting string. Uses the same directives as in the time.strftime\n" 1701 " function in the standard Python time module. In addition, a few other\n" 1702 " directives are supported:\n" 1703 " '%q' - the 'quarter' of the date\n" 1704 " '%f' - Year without century as a decimal number [00,99]. The\n" 1705 " 'year' in this case is the year of the date determined by\n" 1706 " the year for the current quarter. This is the same as %y\n" 1707 " unless the Date is one of the 'qtr-s' frequencies\n" 1708 " '%F' - Year with century as a decimal number. The 'year' in this\n" 1709 " case is the year of the date determined by the year for\n" 1710 " the current quarter. This is the same as %Y unless the\n" 1711 " Date is one of the 'qtr-s' frequencies\n"; 1711 1712 static PyObject * 1712 1713 DateObject_strftime(DateObject *self, PyObject *args) trunk/timeseries/scikits/timeseries/tdates.py
r1204 r1208 109 109 ----- 110 110 If it is currently Saturday or Sunday, then the preceding Friday will be 111 returned. If it is later than the specified day_end_hour and day_end_min,112 now('Business') will be returned. Otherwise, now('Business')-1 will be113 returned.111 returned. If it is later than the specified ``day_end_hour`` and ``day_end_min``, 112 ``now('Business')`` will be returned. 113 Otherwise, ``now('Business')-1`` will be returned. 114 114 115 115 """ … … 185 185 When viewed element-wise, DateArray is a sequence of dates. 186 186 For example, a test such as : 187 187 188 >>> DateArray(...) = value 189 188 190 will be valid only if value is an integer, not a Date 189 191 However, a loop such as : 192 190 193 >>> for d in DateArray(...): 194 191 195 accesses the array element by element. Therefore, `d` is a Date object. 192 196 """ … … 643 647 Parameters 644 648 ---------- 645 dlist : {list of dates or DateArray} (optional) 646 may be a list of dates, integer representations of dates for a given 647 frequency, datetime objects, or an existing DateArray. If specifying 648 a list of dates, you must also specify the `freq` parameter. 649 650 start_date : {Date} (optional) 651 if you want a continuous DateArray, specify a start_date and either an 652 `end_date` or a `length`. Frequency of the resulting DateArray will be 653 automatically determined based on the frequency of this parameter. 654 649 dlist : {sequence, DateArray}, optional 650 A list of dates, integer representations of dates for a given 651 frequency, datetime objects, or an existing DateArray. 652 If `dlist` is a list of dates, the `freq` parameter must also be given. 653 start_date : {Date}, optional 654 First date of a continuous DateArray. 655 This parameter is used only if `dlist` is None. In that case, an ending 656 date (`end_date`) or the length of the array must be given. 657 The frequency of the output will be the frequency of this parameter. 655 658 end_date : {Date} (optional) 656 last date in resulting DateArray. Specify this parameter or `length`659 Last date of the output. Specify this parameter or `length` 657 660 in combination with `start_date` for a continuous DateArray. 658 659 661 length : {int} (optional) 660 the length of the resulting DateArray. Specify this parameter or662 Length of the output. Specify this parameter or 661 663 `end_date` in combination with `start_date` for a continuous DateArray. 664 665 Returns 666 ------- 667 A :class:`DateArray` object 662 668 """ 663 669 freq = check_freq(freq) trunk/timeseries/scikits/timeseries/tseries.py
r1204 r1208 1 1 """ 2 The `TimeSeries` class provides a base for the definition of time series.2 The :class:`TimeSeries` class provides a base for the definition of time series. 3 3 A time series is defined here as the combination of two arrays: 4 4 5 - an array storing the time information (as a `DateArray` instance);6 - an array storing the data (as a `MaskedArray` instance.)5 - an array storing the time information (as a `DateArray` instance); 6 - an array storing the data (as a `MaskedArray` instance.) 7 7 8 8 These two classes were liberally adapted from `MaskedArray` class. 9 9 10 10 11 :author: Pierre GF Gerard-Marchant & Matt Knox … … 280 281 ##### ------------------------------------------------------------------------ 281 282 class _tsmathmethod(object): 282 """Defines a wrapper for arithmetic array methods (add, mul...). 283 When called, returns a new TimeSeries object, with the new series the result 284 of the method applied on the original series. The `_dates` part remains 285 unchanged. 286 """ 283 """ 284 Defines a wrapper for arithmetic array methods (add, mul...). 285 When called, returns a new TimeSeries object, with the new series the result 286 of the method applied on the original series. The `_dates` part remains 287 unchanged. 288 """ 287 289 def __init__ (self, methodname): 288 self._name = self.__name__ = methodname 290 self.__name__ = methodname 291 self.__doc__ = getattr(MaskedArray, methodname).__doc__ 289 292 self.obj = None 290 293 … … 301 304 else: 302 305 compat = True 303 func = getattr(super(TimeSeries, instance), self._ name)306 func = getattr(super(TimeSeries, instance), self.__name__) 304 307 if compat: 305 308 result = np.array(func(other, *args), subok=True).view(type(instance)) … … 320 323 """ 321 324 def __init__ (self, methodname, ondates=False): 322 """abfunc(fillx, filly) must be defined. 323 abinop(x, filly) = x for all x to enable reduce. 324 """ 325 self._name = self.__name__ = methodname 325 self.__name__ = methodname 326 self.__doc__ = getattr(MaskedArray, methodname).__doc__ 326 327 self._ondates = ondates 327 328 self.obj = None … … 333 334 def __call__ (self, *args): 334 335 "Execute the call behavior." 335 _name = self._ name336 _name = self.__name__ 336 337 instance = self.obj 337 338 func_series = getattr(super(TimeSeries, instance), _name) … … 355 356 abinop(x, filly) = x for all x to enable reduce. 356 357 """ 357 self._name = self.__name__ = methodname 358 self.__name__ = methodname 359 self.__doc__ = getattr(MaskedArray, methodname).__doc__ 358 360 self.obj = None 359 361 … … 365 367 "Execute the call behavior." 366 368 (_dates, _series) = (self.obj._dates, self.obj._series) 367 func = getattr(_series, self._ name)369 func = getattr(_series, self.__name__) 368 370 result = func(*args, **params) 369 371 if _dates.size == _series.size: … … 383 385 384 386 class TimeSeries(MaskedArray, object): 385 """Base class for the definition of time series. 386 387 A time series is here defined as the combination of two arrays: 388 * series : {MaskedArray} 389 Data part 390 * dates : {DateArray} 391 Date part 392 393 *Construction*: 387 """ 388 Base class for the definition of time series. 389 390 391 Parameters 392 ---------- 394 393 data : {array_like} 395 data portion of the array. Any data that is valid for constructing a396 MaskedArray can be used here.394 Data portion of the array. 395 Any data that is valid for constructing a MaskedArray can be used here. 397 396 dates : {DateArray} 398 399 *Other Parameters*: 400 all other parameters are the same as for MaskedArray. Please see the 401 documentation for the MaskedArray class in the numpy.ma module 402 for details. 403 404 *Notes*: 405 it is typically recommended to use the `time_series` function for 406 construction as it allows greater flexibility and convenience. 407 """ 397 A :class:`DateArray` instance. 398 399 Other Parameters 400 ---------------- 401 All other parameters are the same as for MaskedArray. 402 Please refer to the documentation for the :class:`MaskedArray` class in the 403 :mod:`numpy.ma` module for details. 404 405 Notes 406 ----- 407 It is recommended to use the `time_series` function for construction, 408 as it is more flexibile and convenient. 409 410 """ 408 411 def __new__(cls, data, dates, mask=nomask, dtype=None, copy=False, 409 412 fill_value=None, subok=True, keep_mask=True, hard_mask=False, … … 716 719 mean = _tsaxismethod('mean') 717 720 var = _tsaxismethod('var') 718 varu = _tsaxismethod('varu')719 721 std = _tsaxismethod('std') 720 stdu = _tsaxismethod('stdu')721 722 all = _tsaxismethod('all') 722 723 any = _tsaxismethod('any') … … 874 875 #..................................................... 875 876 def asfreq(self, freq, relation="END"): 876 """Converts the dates portion of the TimeSeries to another frequency. 877 878 The resulting TimeSeries will have the same shape and dimensions as the 879 original series (unlike the `convert` method). 877 """ 878 Converts the dates portion of the TimeSeries to another frequency. 879 880 The resulting TimeSeries will have the same shape and dimensions as the 881 original series (unlike the `convert` method). 880 882 881 883 Parameters … … 886 888 Returns 887 889 ------- 888 A new TimeSeries with the .dates DateArray at the specified frequency (the 889 `.asfreq` method of the .dates property will be called). The data in the 890 resulting series will be a VIEW of the original series. 890 TimeSeries: 891 A new TimeSeries with the :attr:`.dates` :class:`DateArray` at the 892 specified frequency (the :meth`.asfreq` method of the :attr:`.dates` 893 property will be called). 894 The data in the resulting series will be a VIEW of the original series. 891 895 892 896 Notes 893 897 ----- 894 The parameters are the exact same as for DateArray.asfreq , please see the 895 __doc__ string for that method for details on the parameters and how the 896 actual conversion is performed. 897 """ 898 The parameters are the exact same as for :meth:`DateArray.asfreq`, please 899 see the `__doc__` string for that method for details on the parameters and 900 how the actual conversion is performed. 901 902 """ 898 903 if freq is None: return self 899 904 … … 945 950 def filled(self, fill_value=None): 946 951 """ 947 948 952 Returns an array of the same class as `_data`, with masked values 949 953 filled with `fill_value`. Subclassing is preserved. … … 954 958 The value to fill in masked values with. 955 959 If `fill_value` is None, uses self.fill_value. 956 """ 960 961 """ 957 962 result = self._series.filled(fill_value=fill_value).view(type(self)) 958 963 result._dates = self._dates … … 963 968 Returns the dates and data portion of the TimeSeries "zipped" up in 964 969 a list of standard python objects (eg. datetime, int, etc...). 965 966 """970 971 """ 967 972 if self.ndim > 0: 968 973 return zip(self.dates.tolist(), self.series.tolist()) … … 1128 1133 """ 1129 1134 def __init__(self, methodname): 1130 self._ methodname= methodname1135 self.__name__ = methodname 1131 1136 self.__doc__ = self.getdoc() 1132 1137 def getdoc(self): 1133 1138 "Returns the doc of the function (from the doc of the method)." 1134 1139 try: 1135 return getattr(TimeSeries, self._ methodname).__doc__1140 return getattr(TimeSeries, self.__name__).__doc__ 1136 1141 except: 1137 1142 return "???" 1138 1143 # 1139 1144 def __call__ (self, caller, *args, **params): 1140 if hasattr(caller, self._ methodname):1141 method = getattr(caller, self._ methodname)1145 if hasattr(caller, self.__name__): 1146 method = getattr(caller, self.__name__) 1142 1147 # If method is not callable, it's a property, and don't call it 1143 1148 if hasattr(method, '__call__'): 1144 1149 return method.__call__(*args, **params) 1145 1150 return method 1146 method = getattr(np.asarray(caller), self._ methodname)1151 method = getattr(np.asarray(caller), self.__name__) 1147 1152 try: 1148 1153 return method(*args, **params) 1149 1154 except SystemError: 1150 return getattr(np,self._ methodname).__call__(caller, *args, **params)1155 return getattr(np,self.__name__).__call__(caller, *args, **params) 1151 1156 #............................ 1152 1157 weekday = _frommethod('weekday') … … 1167 1172 #---- ... Additional methods ... 1168 1173 ##### --------------------------------------------------------------------------- 1169 def tofile(se lf, fileobject, format=None,1174 def tofile(series, fileobject, format=None, 1170 1175 separator=" ", linesep='\n', precision=5, 1171 1176 suppress_small=False, keep_open=False): 1172 """Writes the TimeSeries to a file. The series should be 2D at most 1173 1174 *Parameters*: 1177 """ 1178 Writes the TimeSeries to a file. The series should be 2D at most. 1179 1180 Parameters 1181 ---------- 1175 1182 series : {TimeSeries} 1176 1183 The array to write. 1177 fileobject :1184 fileobject 1178 1185 An open file object or a string to a valid filename. 1179 format : {string} 1180 Format string for the date. If None, uses the default date format. 1181 separator : {string} 1186 format : {None, string}, optional 1187 Format string for the date. 1188 If None, uses the default date format. 1189 separator : {string}, optional 1182 1190 Separator to write between elements of the array. 1183 linesep : {string} 1191 linesep : {string}, optional 1184 1192 Separator to write between rows of array. 1185 precision : {integer} 1193 precision : {integer}, optional 1186 1194 Number of digits after the decimal place to write. 1187 suppress_small : {boolean} 1195 suppress_small : {boolean}, optional 1188 1196 Whether on-zero to round small numbers down to 0.0 1189 keep_open : {boolean} 1197 keep_open : {boolean}, optional 1190 1198 Whether to close the file or to return the open file. 1191 1199 1192 *Returns*: 1200 Returns 1201 ------- 1193 1202 file : {file object} 1194 1203 The open file (if keep_open is non-zero) … … 1200 1209 raise ImportError("scipy is required for the tofile function/method") 1201 1210 1202 (_dates, _data) = (se lf._dates, self._series)1211 (_dates, _data) = (series._dates, series._series) 1203 1212 optpars = dict(separator=separator,linesep=linesep,precision=precision, 1204 1213 suppress_small=suppress_small,keep_open=keep_open) … … 1225 1234 #............................................ 1226 1235 def asrecords(series): 1227 """Returns the masked time series as a recarray. 1228 Fields are `_dates`, `_data` and _`mask`. 1229 """ 1236 """ 1237 Returns the time series as a recarray. 1238 1239 Fields are ``_dates``, ``_data`` and ``_mask``. 1240 1241 """ 1230 1242 desctype = [('_dates',int_), ('_series',series.dtype), ('_mask', bool_)] 1231 1243 flat = series.ravel() … … 1243 1255 1244 1256 def flatten(series): 1245 """Flattens a (multi-) time series to 1D series.""" 1257 """ 1258 Flattens a (multi-) time series to 1D series. 1259 1260 """ 1246 1261 shp_ini = series.shape 1247 1262 # Already flat time series.... … … 1256 1271 newseries = series._series.reshape(newshape) 1257 1272 return time_series(newseries, newdates) 1273 1258 1274 TimeSeries.flatten = flatten 1259 1275 … … 1264 1280 dtype=None, copy=False, fill_value=None, keep_mask=True, 1265 1281 hard_mask=False): 1266 """Creates a TimeSeries object 1282 """ 1283 Creates a TimeSeries object 1267 1284 1268 1285 Parameters … … 1280 1297 A valid frequency specification 1281 1298 1282 *Other Parameters*: 1283 All other parameters that are accepted by the *array* function in the 1284 numpy.ma module are also accepted by this function. 1285 1286 *Notes*: 1287 the date portion of the time series must be specified in one of the 1299 Other Parameters 1300 ---------------- 1301 All other parameters that are accepted by the :func:`numpy.ma.array` function 1302 in the :mod:`numpy.ma` module are also accepted by this function. 1303 1304 Notes 1305 ----- 1306 The date portion of the time series must be specified in one of the 1288 1307 following ways: 1289 - specify a TimeSeries object for the *data*parameter1290 - pass a DateArray for the *dates*parameter1308 - specify a TimeSeries object for the `data` parameter 1309 - pass a DateArray for the `dates` parameter 1291 1310 - specify a start_date (a continuous DateArray will be automatically 1292 1311 constructed for the dates portion) 1293 1312 - specify just a frequency (for TimeSeries of size zero) 1294 """ 1313 1314 """ 1295 1315 maparms = dict(copy=copy, dtype=dtype, fill_value=fill_value, subok=True, 1296 1316 keep_mask=keep_mask, hard_mask=hard_mask,) … … 1552 1572 1553 1573 def convert(series, freq, func=None, position='END', *args, **kwargs): 1554 """Converts a series to a frequency. Private function called by convert1574 """Converts a series from one frequency to another. 1555 1575 1556 1576 Parameters … … 1563 1583 specification (string or integer) 1564 1584 func : {None,function}, optional 1565 When converting to a lower frequency, funcis a function that acts on1566 one date's worth of data. funcshould handle masked values appropriately.1567 If funcis None, then each data point in the resulting series will a1585 When converting to a lower frequency, `func` is a function that acts on 1586 one date's worth of data. `func` should handle masked values appropriately. 1587 If `func` is None, then each data point in the resulting series will a 1568 1588 group of data points that fall into the date at the lower frequency. 1569 1589 … … 1839 1859 1840 1860 def concatenate(series, axis=0, remove_duplicates=True, fill_missing=False): 1841 """Joins series together. 1842 1843 The series are joined in chronological order. Duplicated dates are handled with 1844 the `remove_duplicates` parameter. If remove_duplicate=False, duplicated dates are 1845 saved. Otherwise, only the first occurence of the date is conserved. 1846 1847 Example 1848 >>> a = time_series([1,2,3], start_date=now('D')) 1849 >>> b = time_series([10,20,30], start_date=now('D')+1) 1850 >>> c = concatenate((a,b)) 1851 >>> c._series 1852 masked_array(data = [ 1 2 3 30], 1853 mask = False, 1854 fill_value=999999) 1861 """ 1862 Joins series together. 1863 1864 The series are joined in chronological order. 1865 Duplicated dates are handled with the `remove_duplicates` parameter. 1866 If `remove_duplicate` is False, duplicated dates are saved. 1867 Otherwise, only the first occurence of the date is conserved. 1855 1868 1856 1869 … … 1865 1878 fill_missing : {False, True}, optional 1866 1879 Whether to fill the missing dates with missing values. 1880 1881 Example 1882 ------- 1883 >>> a = time_series([1,2,3], start_date=now('D')) 1884 >>> b = time_series([10,20,30], start_date=now('D')+1) 1885 >>> c = concatenate((a,b)) 1886 >>> c._series 1887 masked_array(data = [ 1 2 3 30], 1888 mask = False, 1889 fill_value=999999) 1890 1867 1891 """ 1868 1892 # Get the common frequency, raise an error if incompatibility
