[Scipy-svn] r3570 - in trunk/scipy/sandbox/timeseries: . include lib src tests

scipy-svn@scip... scipy-svn@scip...
Fri Nov 23 15:42:35 CST 2007


Author: mattknox_ca
Date: 2007-11-23 15:42:20 -0600 (Fri, 23 Nov 2007)
New Revision: 3570

Modified:
   trunk/scipy/sandbox/timeseries/dates.py
   trunk/scipy/sandbox/timeseries/include/c_dates.h
   trunk/scipy/sandbox/timeseries/lib/moving_funcs.py
   trunk/scipy/sandbox/timeseries/plotlib.py
   trunk/scipy/sandbox/timeseries/report.py
   trunk/scipy/sandbox/timeseries/src/c_dates.c
   trunk/scipy/sandbox/timeseries/src/cseries.c
   trunk/scipy/sandbox/timeseries/tests/test_dates.py
   trunk/scipy/sandbox/timeseries/tseries.py
Log:
- renamed 'thisday' to 'now' (left 'thisday' and 'today' as deprecated aliases)
- added tolist method to DateArray which returns a list of datetime objects
- added additional documentation
- renamed 'day_of_week' to 'weekday' (left 'day_of_week' as deprecated alias for now)

Modified: trunk/scipy/sandbox/timeseries/dates.py
===================================================================
--- trunk/scipy/sandbox/timeseries/dates.py	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/dates.py	2007-11-23 21:42:20 UTC (rev 3570)
@@ -12,6 +12,7 @@
 
 import datetime as dt
 
+import operator
 import itertools
 import warnings
 import types
@@ -36,17 +37,21 @@
 cseries.set_callback_DateFromString(DateFromString)
 cseries.set_callback_DateTimeFromString(DateTimeFromString)
 
-from cseries import Date, thisday, check_freq, check_freq_str, get_freq_group,\
+from cseries import Date, now, check_freq, check_freq_str, get_freq_group,\
                     DateCalc_Error, DateCalc_RangeError
-today = thisday
 
+# aliases for `now` function. These are deprecated
+today = now
+thisday = now
+
 __all__ = [
 'Date', 'DateArray','isDate','isDateArray',
 'DateError', 'ArithmeticDateError', 'FrequencyDateError','InsufficientDateError',
 'datearray','date_array', 'date_array_fromlist', 'date_array_fromrange',
-'day_of_week','day_of_year','day','month','quarter','year','hour','minute',
-'second','thisday','today','prevbusday','period_break', 'check_freq',
-'check_freq_str','get_freq_group', 'DateCalc_Error', 'DateCalc_RangeError'
+'day_of_week','weekday','day_of_year','day','month','quarter','year','hour',
+'minute','second','now','thisday','today','prevbusday','period_break',
+'check_freq','check_freq_str','get_freq_group', 'DateCalc_Error',
+'DateCalc_RangeError'
            ]
 
 
@@ -93,22 +98,23 @@
 def prevbusday(day_end_hour=18, day_end_min=0):
     """Returns the previous business day (Monday-Friday) at business frequency.
 
-:Parameters:
-    - day_end_hour : (int, *[18]* )
-    - day_end_min : (int, *[0]*)
+*Parameters*:
+    day_end_hour : {18, int} (optional)
+    day_end_min : {0, int} (optional)
 
-:Return values:
+*Return values*:
     If it is currently Saturday or Sunday, then the preceding Friday will be
     returned. If it is later than the specified day_end_hour and day_end_min,
-    thisday('b') will be returned. Otherwise, thisday('b')-1 will be returned.
+    now('Business') will be returned. Otherwise, now('Business')-1 will be
+    returned.
 """
     tempDate = dt.datetime.now()
     dateNum = tempDate.hour + float(tempDate.minute)/60
     checkNum = day_end_hour + float(day_end_min)/60
     if dateNum < checkNum:
-        return thisday(_c.FR_BUS) - 1
+        return now(_c.FR_BUS) - 1
     else:
-        return thisday(_c.FR_BUS)
+        return now(_c.FR_BUS)
 
 
 def isDate(data):
@@ -283,9 +289,11 @@
         "Returns the day of month."
         return self.__getdateinfo__('D')
     @property
-    def day_of_week(self):
+    def weekday(self):
         "Returns the day of week."
         return self.__getdateinfo__('W')
+    # deprecated alias for weekday
+    day_of_week = weekday
     @property
     def day_of_year(self):
         "Returns the day of year."
@@ -329,7 +337,7 @@
         return self.__getdateinfo__('I')
 
     days = day
-    weekdays = day_of_week
+    weekdays = weekday
     yeardays = day_of_year
     months = month
     quarters = quarter
@@ -355,7 +363,6 @@
         "Converts the dates from values to ordinals."
         # Note: we better try to cache the result
         if self._cachedinfo['toord'] is None:
-#            diter = (Date(self.freq, value=d).toordinal() for d in self)
             if self.freq == _c.FR_UND:
                 diter = (d.value for d in self)
             else:
@@ -364,6 +371,13 @@
             self._cachedinfo['toord'] = toord
         return self._cachedinfo['toord']
     #
+    def tolist(self):
+        """Returns a hierarchical python list of standard datetime objects."""
+        _result = numpy.empty(self.shape, dtype=numpy.object_)
+        for idx, val in numpy.ndenumerate(self):
+            operator.setitem(_result, idx, Date(freq=self.freq, value=val).datetime)
+        return _result.tolist()
+    #
     def tostring(self):
         "Converts the dates to strings."
         # Note: we better cache the result
@@ -674,7 +688,9 @@
         except SystemError:
             return getattr(numpy,self._methodname).__call__(caller, *args, **params)
 #............................
-day_of_week = _frommethod('day_of_week')
+weekday = _frommethod('day_of_week')
+# deprecated alias for weekday
+day_of_week = weekday
 day_of_year = _frommethod('day_of_year')
 year = _frommethod('year')
 quarter = _frommethod('quarter')

Modified: trunk/scipy/sandbox/timeseries/include/c_dates.h
===================================================================
--- trunk/scipy/sandbox/timeseries/include/c_dates.h	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/include/c_dates.h	2007-11-23 21:42:20 UTC (rev 3570)
@@ -110,7 +110,7 @@
 PyObject *DateArray_getDateInfo(PyObject *, PyObject *);
 
 
-PyObject *c_dates_thisday(PyObject *, PyObject *);
+PyObject *c_dates_now(PyObject *, PyObject *);
 PyObject *c_dates_check_freq(PyObject *, PyObject *);
 PyObject *c_dates_check_freq_str(PyObject *, PyObject *);
 PyObject *c_dates_get_freq_group(PyObject *, PyObject *);

Modified: trunk/scipy/sandbox/timeseries/lib/moving_funcs.py
===================================================================
--- trunk/scipy/sandbox/timeseries/lib/moving_funcs.py	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/lib/moving_funcs.py	2007-11-23 21:42:20 UTC (rev 3570)
@@ -366,10 +366,10 @@
 
 ###############################################################################
 if __name__ == '__main__':
-    from timeseries import time_series, today
+    from timeseries import time_series, now
     from maskedarray.testutils import assert_equal, assert_almost_equal
     #
-    series = time_series(N.arange(10),start_date=today('D'))
+    series = time_series(N.arange(10),start_date=now('D'))
     #
     filtered = mov_sum(series,3)
     assert_equal(filtered, [0,1,3,6,9,12,15,18,21,24])

Modified: trunk/scipy/sandbox/timeseries/plotlib.py
===================================================================
--- trunk/scipy/sandbox/timeseries/plotlib.py	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/plotlib.py	2007-11-23 21:42:20 UTC (rev 3570)
@@ -574,7 +574,7 @@
         if self.isminor:
             fmt = self.formatdict.pop(x, '')
             if fmt is not '':
-                retval = Date(self.freq, value=int(x)).strfmt(fmt)
+                retval = Date(self.freq, value=int(x)).strftime(fmt)
             else:
                 retval = ''
         else:

Modified: trunk/scipy/sandbox/timeseries/report.py
===================================================================
--- trunk/scipy/sandbox/timeseries/report.py	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/report.py	2007-11-23 21:42:20 UTC (rev 3570)
@@ -328,7 +328,7 @@
         if datefmt is None:
             def datefmt_func(date): return str(date)
         else:
-            def datefmt_func(date): return date.strfmt(datefmt)
+            def datefmt_func(date): return date.strftime(datefmt)
 
         if dates is None:
             tseries = ts.align_series(*tseries)

Modified: trunk/scipy/sandbox/timeseries/src/c_dates.c
===================================================================
--- trunk/scipy/sandbox/timeseries/src/c_dates.c	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/src/c_dates.c	2007-11-23 21:42:20 UTC (rev 3570)
@@ -1649,9 +1649,12 @@
 }
 
 static char DateObject_strfmt_doc[] =
+"Deprecated alias for strftime method";
+
+static char DateObject_strftime_doc[] =
 "Returns string representation of Date object according to format specified.\n\n"
-":Parameters:\n"
-"   - fmt : string\n"
+"*Parameters*:\n"
+"   fmt : {str}\n"
 "       Formatting string. Uses the same directives as in the time.strftime\n"
 "       function in the standard Python time module. In addition, a few other\n"
 "       directives are supported:\n"
@@ -1665,7 +1668,7 @@
 "                 the current quarter. This is the same as %Y unless the\n"
 "                 Date is one of the 'qtr-s' frequencies\n";
 static PyObject *
-DateObject_strfmt(DateObject *self, PyObject *args)
+DateObject_strftime(DateObject *self, PyObject *args)
 {
 
     char *orig_fmt_str, *fmt_str;
@@ -1689,7 +1692,7 @@
     long (*toDaily)(long, char, asfreq_info*) = NULL;
     asfreq_info af_info;
 
-    if (!PyArg_ParseTuple(args, "s:strfmt(fmt)", &orig_fmt_str)) return NULL;
+    if (!PyArg_ParseTuple(args, "s:strftime(fmt)", &orig_fmt_str)) return NULL;
 
     toDaily = get_asfreq_func(self->freq, FR_DAY, 0);
     get_asfreq_info(self->freq, FR_DAY, &af_info);
@@ -1821,7 +1824,7 @@
 
     if (string_arg == NULL) { return NULL; }
 
-    retval = DateObject_strfmt(self, string_arg);
+    retval = DateObject_strftime(self, string_arg);
     Py_DECREF(string_arg);
 
     return retval;
@@ -2098,7 +2101,7 @@
 }
 
 static PyObject *
-DateObject_day_of_week(DateObject *self, void *closure) {
+DateObject_weekday(DateObject *self, void *closure) {
     struct date_info dinfo;
     if(DateObject_set_date_info(self, &dinfo) == -1) return NULL;
     return PyInt_FromLong(dinfo.day_of_week);
@@ -2173,8 +2176,11 @@
             "Returns the week.", NULL},
     {"day", (getter)DateObject_day, (setter)DateObject_ReadOnlyErr,
             "Returns the day of month.", NULL},
-    {"day_of_week", (getter)DateObject_day_of_week, (setter)DateObject_ReadOnlyErr,
+    {"weekday", (getter)DateObject_weekday, (setter)DateObject_ReadOnlyErr,
             "Returns the day of week.", NULL},
+	// deprecated alias for weekday property
+    {"day_of_week", (getter)DateObject_weekday, (setter)DateObject_ReadOnlyErr,
+            "Returns the day of week.", NULL},
     {"day_of_year", (getter)DateObject_day_of_year, (setter)DateObject_ReadOnlyErr,
             "Returns the day of year.", NULL},
     {"second", (getter)DateObject_second, (setter)DateObject_ReadOnlyErr,
@@ -2223,7 +2229,10 @@
 static PyMethodDef DateObject_methods[] = {
     {"toordinal", (PyCFunction)DateObject_toordinal, METH_NOARGS,
      DateObject_toordinal_doc},
-    {"strfmt", (PyCFunction)DateObject_strfmt, METH_VARARGS,
+    {"strftime", (PyCFunction)DateObject_strftime, METH_VARARGS,
+     DateObject_strftime_doc},
+    // deprecated alias for strftime
+    {"strfmt", (PyCFunction)DateObject_strftime, METH_VARARGS,
      DateObject_strfmt_doc},
     {"asfreq", (PyCFunction)DateObject_asfreq, METH_VARARGS | METH_KEYWORDS,
      DateObject_asfreq_doc},
@@ -2331,7 +2340,7 @@
 }
 
 PyObject *
-c_dates_thisday(PyObject *self, PyObject *args) {
+c_dates_now(PyObject *self, PyObject *args) {
 
     PyObject *freq, *init_args, *init_kwargs;
     time_t rawtime;
@@ -2340,7 +2349,7 @@
 
     DateObject *secondly_date;
 
-    if (!PyArg_ParseTuple(args, "O:thisday(freq)", &freq)) return NULL;
+    if (!PyArg_ParseTuple(args, "O:now(freq)", &freq)) return NULL;
 
     if ((freq_val = check_freq(freq)) == INT_ERR_CODE) return NULL;
 
@@ -2635,7 +2644,7 @@
             skip_periods = __skip_periods_day(freq);
             break;
         case 'W': //day of week
-            getDateInfo = &DateObject_day_of_week;
+            getDateInfo = &DateObject_weekday;
             skip_periods = __skip_periods_day(freq);
             break;
         case 'I': //week of year

Modified: trunk/scipy/sandbox/timeseries/src/cseries.c
===================================================================
--- trunk/scipy/sandbox/timeseries/src/cseries.c	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/src/cseries.c	2007-11-23 21:42:20 UTC (rev 3570)
@@ -26,11 +26,11 @@
      METH_VARARGS, ""},
 
 
-    {"thisday", (PyCFunction)c_dates_thisday,
+    {"now", (PyCFunction)c_dates_now,
      METH_VARARGS,
-        "Returns today's date, at the given frequency\n\n"
-        ":Parameters:\n"
-        "   - freq : string/int\n"
+        "Returns the current date/time, at the given frequency\n\n"
+        "*Parameters*:\n"
+        "   freq : {freq_spec}\n"
         "       Frequency to convert the Date to. Accepts any valid frequency\n"
         "       specification (string or integer)\n"},
 

Modified: trunk/scipy/sandbox/timeseries/tests/test_dates.py
===================================================================
--- trunk/scipy/sandbox/timeseries/tests/test_dates.py	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/tests/test_dates.py	2007-11-23 21:42:20 UTC (rev 3570)
@@ -29,7 +29,7 @@
 from timeseries import const as C
 from timeseries.parser import DateFromString, DateTimeFromString
 from timeseries import Date, DateArray,\
-    thisday, today, date_array, date_array_fromlist
+    now, date_array, date_array_fromlist
 from timeseries.cseries import freq_dict
 
 
@@ -122,8 +122,8 @@
         freqs = [x[0] for x in freq_dict.values() if x[0] != 'U']
 
         for f in freqs:
-            today = thisday(f)
-            assert_equal(Date(freq=f, value=today.value), today)
+            _now = now(f)
+            assert_equal(Date(freq=f, value=_now.value), _now)
         print "finished test_consistent_value"
 
     def test_shortcuts(self):
@@ -134,14 +134,14 @@
         assert_equal(Date('D','2007-01'), Date('D', value=732677))
         assert_equal(Date('D',732677), Date('D', value=732677))
         # DateArray shortcuts
-        n = today('M')
+        n = now('M')
         d = date_array(start_date=n, length=3)
         assert_equal(date_array(n,length=3), d)
         assert_equal(date_array(n, n+2), d)
         print "finished test_shortcuts"
 
 class TestDateProperties(NumpyTestCase):
-    "Test properties such as year, month, day_of_week, etc...."
+    "Test properties such as year, month, weekday, etc...."
 
     def __init__(self, *args, **kwds):
         NumpyTestCase.__init__(self, *args, **kwds)
@@ -201,21 +201,21 @@
         assert_equal(b_date.quarter, 1)
         assert_equal(b_date.month, 1)
         assert_equal(b_date.day, 1)
-        assert_equal(b_date.day_of_week, 0)
+        assert_equal(b_date.weekday, 0)
         assert_equal(b_date.day_of_year, 1)
 
         assert_equal(d_date.year, 2007)
         assert_equal(d_date.quarter, 1)
         assert_equal(d_date.month, 1)
         assert_equal(d_date.day, 1)
-        assert_equal(d_date.day_of_week, 0)
+        assert_equal(d_date.weekday, 0)
         assert_equal(d_date.day_of_year, 1)
 
         assert_equal(h_date.year, 2007)
         assert_equal(h_date.quarter, 1)
         assert_equal(h_date.month, 1)
         assert_equal(h_date.day, 1)
-        assert_equal(h_date.day_of_week, 0)
+        assert_equal(h_date.weekday, 0)
         assert_equal(h_date.day_of_year, 1)
         assert_equal(h_date.hour, 0)
 
@@ -223,7 +223,7 @@
         assert_equal(t_date.quarter, 1)
         assert_equal(t_date.month, 1)
         assert_equal(t_date.day, 1)
-        assert_equal(t_date.day_of_week, 0)
+        assert_equal(t_date.weekday, 0)
         assert_equal(t_date.day_of_year, 1)
         assert_equal(t_date.hour, 0)
         assert_equal(t_date.minute, 0)
@@ -232,7 +232,7 @@
         assert_equal(s_date.quarter, 1)
         assert_equal(s_date.month, 1)
         assert_equal(s_date.day, 1)
-        assert_equal(s_date.day_of_week, 0)
+        assert_equal(s_date.weekday, 0)
         assert_equal(s_date.day_of_year, 1)
         assert_equal(s_date.hour, 0)
         assert_equal(s_date.minute, 0)
@@ -483,17 +483,17 @@
             date_W_to_Q = dWrap(Date(freq='Q', year=2007, quarter=1))
             date_W_to_M = dWrap(Date(freq='M', year=2007, month=1))
 
-            if Date(freq='D', year=2007, month=12, day=31).day_of_week == 6:
+            if Date(freq='D', year=2007, month=12, day=31).weekday == 6:
                 date_W_to_A_end_of_year = dWrap(Date(freq='A', year=2007))
             else:
                 date_W_to_A_end_of_year = dWrap(Date(freq='A', year=2008))
 
-            if Date(freq='D', year=2007, month=3, day=31).day_of_week == 6:
+            if Date(freq='D', year=2007, month=3, day=31).weekday == 6:
                 date_W_to_Q_end_of_quarter = dWrap(Date(freq='Q', year=2007, quarter=1))
             else:
                 date_W_to_Q_end_of_quarter = dWrap(Date(freq='Q', year=2007, quarter=2))
 
-            if Date(freq='D', year=2007, month=1, day=31).day_of_week == 6:
+            if Date(freq='D', year=2007, month=1, day=31).weekday == 6:
                 date_W_to_M_end_of_month = dWrap(Date(freq='M', year=2007, month=1))
             else:
                 date_W_to_M_end_of_month = dWrap(Date(freq='M', year=2007, month=2))
@@ -877,7 +877,7 @@
         assert_equal(empty_darray.get_steps(), None)
 
     def test_cachedinfo(self):
-        D = date_array(start_date=thisday('D'), length=5)
+        D = date_array(start_date=now('D'), length=5)
         Dstr = D.tostring()
         assert_equal(D.tostring(), Dstr)
         DL = D[[0,-1]]

Modified: trunk/scipy/sandbox/timeseries/tseries.py
===================================================================
--- trunk/scipy/sandbox/timeseries/tseries.py	2007-11-23 07:21:55 UTC (rev 3569)
+++ trunk/scipy/sandbox/timeseries/tseries.py	2007-11-23 21:42:20 UTC (rev 3570)
@@ -35,7 +35,7 @@
 import dates
 from dates import DateError, InsufficientDateError
 from dates import Date, isDate, DateArray, isDateArray, \
-    date_array, date_array_fromlist, date_array_fromrange, thisday, today, \
+    date_array, date_array_fromlist, date_array_fromrange, now, \
     check_freq, check_freq_str
 
 import cseries
@@ -56,6 +56,7 @@
 'quarter',
 'second','split', 'stack',
 'tofile','tshift',
+'week',
 'year',
 ]
 
@@ -595,9 +596,11 @@
         """Returns the day of month for each date in self._dates."""
         return self._dates.day
     @property
-    def day_of_week(self):
+    def weekday(self):
         """Returns the day of week for each date in self._dates."""
-        return self._dates.day_of_week
+        return self._dates.weekday
+    # deprecated alias for weekday
+    day_of_week = weekday
     @property
     def day_of_year(self):
         """Returns the day of year for each date in self._dates."""
@@ -632,7 +635,7 @@
         return self._dates.week
 
     days = day
-    weekdays = day_of_week
+    weekdays = weekday
     yeardays = day_of_year
     months = month
     quarters = quarter
@@ -873,8 +876,11 @@
         except SystemError:
             return getattr(numpy,self._methodname).__call__(caller, *args, **params)
 #............................
-day_of_week = _frommethod('day_of_week')
+weekday = _frommethod('weekday')
+# deprecated alias for weekday 
+day_of_week = weekday
 day_of_year = _frommethod('day_of_year')
+week = _frommethod('week')
 year = _frommethod('year')
 quarter = _frommethod('quarter')
 month = _frommethod('month')
@@ -1544,8 +1550,8 @@
 saved. Otherwise, only the first occurence of the date is conserved.
 
 Example
->>> a = time_series([1,2,3], start_date=today('D'))
->>> b = time_series([10,20,30], start_date=today('D')+1)
+>>> a = time_series([1,2,3], start_date=now('D'))
+>>> b = time_series([10,20,30], start_date=now('D')+1)
 >>> c = concatenate((a,b))
 >>> c._series
 masked_array(data = [ 1  2  3 30],



More information about the Scipy-svn mailing list