[Scipy-svn] r2537 - trunk/Lib/sandbox/timeseries/src

scipy-svn at scipy.org scipy-svn at scipy.org
Thu Jan 11 12:26:05 CST 2007


Author: mattknox_ca
Date: 2007-01-11 12:26:01 -0600 (Thu, 11 Jan 2007)
New Revision: 2537

Modified:
   trunk/Lib/sandbox/timeseries/src/cseries.c
Log:
added support for additional frequencies. Fixed some bugs

Modified: trunk/Lib/sandbox/timeseries/src/cseries.c
===================================================================
--- trunk/Lib/sandbox/timeseries/src/cseries.c	2007-01-11 15:53:21 UTC (rev 2536)
+++ trunk/Lib/sandbox/timeseries/src/cseries.c	2007-01-11 18:26:01 UTC (rev 2537)
@@ -7,6 +7,13 @@
 
 static char cseries_doc[] = "Speed sensitive time series operations";
 
+/*
+these are the earliest values at each frequency that can be converted
+to frequencies higher than daily (ie. Hourly, Minutely, Secondly)
+*/
+
+static long minval_D_toHighFreq = 719163;
+
 ///////////////////////////////////////////////////////////////////////
 
 // helpers for frequency conversion routines //
@@ -83,137 +90,6 @@
 // frequency specifc conversion routines
 // each function must take an integer fromDate and a char relation ('B' or 'A' for 'BEFORE' or 'AFTER')
 
-//************ FROM ANNUAL ***************
-
-static long asfreq_AtoQ(long fromDate, char relation) {
-    if (relation == 'B') { return fromDate * 4 + 1; }
-    else {                 return (fromDate + 1) * 4; }
-}
-
-static long asfreq_AtoM(long fromDate, char relation) {
-    if (relation == 'B') { return fromDate * 12 + 1; }
-    else {                 return (fromDate + 1) * 12; }
-}
-
-static long asfreq_AtoW(long fromDate, char relation) { return 0; }
-
-static long asfreq_AtoB(long fromDate, char relation) {
-    if (relation == 'B') { return busday_before(fromDate,1,1); }
-    else {                 return busday_after(fromDate+1,1,1); }
-}
-
-static long asfreq_AtoD(long fromDate, char relation) {
-    if (relation == 'B') { return absdate_from_ymd(fromDate,1,1); }
-    else {                 return absdate_from_ymd(fromDate+1,1,1) - 1; }
-}
-
-static long asfreq_AtoH(long fromDate, char relation) { return 0; }
-static long asfreq_AtoT(long fromDate, char relation) { return 0; }
-static long asfreq_AtoS(long fromDate, char relation) { return 0; }
-
-
-//************ FROM QUARTERLY ***************
-
-static long asfreq_QtoA(long fromDate, char relation) { return (fromDate - 1) / 4; }
-
-static long asfreq_QtoM(long fromDate, char relation) {
-    if (relation == 'B') { return fromDate * 3 - 2; }
-    else {                 return fromDate  * 3; }
-}
-
-static long asfreq_QtoW(long fromDate, char relation) { return 0; }
-
-static void QtoD_ym(long fromDate, long *y, long *m) {
-    *y = (fromDate - 1) / 4;
-    *m = fromDate * 3 - 12 * (*y) - 2;
-}
-
-static long asfreq_QtoB(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        QtoD_ym(fromDate, &y, &m);
-        return busday_before(y,m,1);
-    } else {
-        QtoD_ym(fromDate+1, &y, &m);
-        return busday_after(y, m, 1);
-    }
-}
-
-static long asfreq_QtoD(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        QtoD_ym(fromDate, &y, &m);
-        return absdate_from_ymd(y, m, 1);
-    } else {
-        QtoD_ym(fromDate+1, &y, &m);
-        return absdate_from_ymd(y, m, 1) - 1;
-    }
-}
-
-static long asfreq_QtoH(long fromDate, char relation) { return 0; }
-static long asfreq_QtoT(long fromDate, char relation) { return 0; }
-static long asfreq_QtoS(long fromDate, char relation) { return 0; }
-
-
-//************ FROM MONTHLY ***************
-
-static long asfreq_MtoA(long fromDate, char relation) { return (fromDate - 1) / 12; }
-static long asfreq_MtoQ(long fromDate, char relation) { return (fromDate - 1) / 3 + 1; }
-
-static long asfreq_MtoW(long fromDate, char relation) { return 0; }
-
-static void MtoD_ym(long fromDate, long *y, long *m) {
-    *y = (fromDate - 1) / 12;
-    *m = fromDate  - 12 * (*y);
-}
-
-static long asfreq_MtoB(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        MtoD_ym(fromDate, &y, &m);
-        return busday_before(y,m,1);
-    } else {
-        MtoD_ym(fromDate+1, &y, &m);
-        return busday_after(y, m, 1);
-    }
-}
-
-static long asfreq_MtoD(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        MtoD_ym(fromDate, &y, &m);
-        return absdate_from_ymd(y, m, 1);
-    } else {
-        MtoD_ym(fromDate+1, &y, &m);
-        return absdate_from_ymd(y, m, 1) - 1;
-    }
-}
-
-static long asfreq_MtoH(long fromDate, char relation) { return 0; }
-static long asfreq_MtoT(long fromDate, char relation) { return 0; }
-static long asfreq_MtoS(long fromDate, char relation) { return 0; }
-
-
-//************ FROM WEEKLY ***************
-
-static long asfreq_WtoA(long fromDate, char relation) { return 0; }
-static long asfreq_WtoQ(long fromDate, char relation) { return 0; }
-static long asfreq_WtoM(long fromDate, char relation) { return 0; }
-static long asfreq_WtoB(long fromDate, char relation) { return 0; }
-static long asfreq_WtoD(long fromDate, char relation) { return 0; }
-static long asfreq_WtoH(long fromDate, char relation) { return 0; }
-static long asfreq_WtoT(long fromDate, char relation) { return 0; }
-static long asfreq_WtoS(long fromDate, char relation) { return 0; }
-
-
 //************ FROM DAILY ***************
 
 static long asfreq_DtoA(long fromDate, char relation) {
@@ -248,7 +124,7 @@
 }
 
 
-static long asfreq_DtoW(long fromDate, char relation) { return 0; }
+static long asfreq_DtoW(long fromDate, char relation) { return -1; }
 
 static long asfreq_DtoB(long fromDate, char relation) {
     mxDateTimeObject *mxDate;
@@ -281,11 +157,67 @@
     return result;
 }
 
+static long asfreq_DtoHIGHFREQ(long fromDate, char relation, long periodsPerDay) {
+    if (fromDate >= minval_D_toHighFreq) {
+        if (relation == 'B') { return (fromDate - minval_D_toHighFreq)*(periodsPerDay) + 1; }
+        else                 { return (fromDate - minval_D_toHighFreq + 1)*(periodsPerDay); }
+    } else { return -1; }
+}
 
-static long asfreq_DtoH(long fromDate, char relation) { return 0; }
-static long asfreq_DtoT(long fromDate, char relation) { return 0; }
-static long asfreq_DtoS(long fromDate, char relation) { return 0; }
+static long asfreq_DtoH(long fromDate, char relation) { return asfreq_DtoHIGHFREQ(fromDate, relation, 24); }
+static long asfreq_DtoT(long fromDate, char relation) { return asfreq_DtoHIGHFREQ(fromDate, relation, 24*60); }
+static long asfreq_DtoS(long fromDate, char relation) { return asfreq_DtoHIGHFREQ(fromDate, relation, 24*60*60); }
 
+//************ FROM SECONDLY ***************
+
+static long asfreq_StoD(long fromDate, char relation) { return (fromDate - 1)/(60*60*24) + minval_D_toHighFreq; }
+
+static long asfreq_StoA(long fromDate, char relation) { return asfreq_DtoA(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoQ(long fromDate, char relation) { return asfreq_DtoQ(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoM(long fromDate, char relation) { return asfreq_DtoM(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoW(long fromDate, char relation) { return asfreq_DtoW(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoB(long fromDate, char relation) { return asfreq_DtoB(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoB_forConvert(long fromDate, char relation)
+    { return asfreq_DtoB_forConvert(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoT(long fromDate, char relation) { return (fromDate - 1)/60 + 1; }
+static long asfreq_StoH(long fromDate, char relation) { return (fromDate - 1)/(60*60) + 1; }
+
+//************ FROM MINUTELY ***************
+
+static long asfreq_TtoD(long fromDate, char relation) { return (fromDate - 1)/(60*24) + minval_D_toHighFreq; }
+
+static long asfreq_TtoA(long fromDate, char relation) { return asfreq_DtoA(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoQ(long fromDate, char relation) { return asfreq_DtoQ(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoM(long fromDate, char relation) { return asfreq_DtoM(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoW(long fromDate, char relation) { return asfreq_DtoW(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoB(long fromDate, char relation) { return asfreq_DtoB(asfreq_TtoD(fromDate, relation), relation); }
+
+static long asfreq_TtoB_forConvert(long fromDate, char relation)
+    { return asfreq_DtoB_forConvert(asfreq_TtoD(fromDate, relation), relation); }
+
+static long asfreq_TtoH(long fromDate, char relation) { return (fromDate - 1)/60 + 1; }
+static long asfreq_TtoS(long fromDate, char relation) {
+    if (relation == 'B') {  return fromDate*60 - 59; }
+    else                 {  return fromDate*60;      }}
+
+//************ FROM HOURLY ***************
+
+static long asfreq_HtoD(long fromDate, char relation) { return (fromDate - 1)/24 + minval_D_toHighFreq; }
+static long asfreq_HtoA(long fromDate, char relation) { return asfreq_DtoA(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoQ(long fromDate, char relation) { return asfreq_DtoQ(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoM(long fromDate, char relation) { return asfreq_DtoM(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoW(long fromDate, char relation) { return asfreq_DtoW(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoB(long fromDate, char relation) { return asfreq_DtoB(asfreq_HtoD(fromDate, relation), relation); }
+
+static long asfreq_HtoB_forConvert(long fromDate, char relation)
+    { return asfreq_DtoB_forConvert(asfreq_HtoD(fromDate, relation), relation); }
+
+// calculation works out the same as TtoS, so we just call that function for HtoT
+static long asfreq_HtoT(long fromDate, char relation) { return asfreq_TtoS(fromDate, relation); }
+static long asfreq_HtoS(long fromDate, char relation) {
+    if (relation == 'B') {  return fromDate*60*60 - 60*60 + 1; }
+    else                 {  return fromDate*60*60;             }}
+
 //************ FROM BUSINESS ***************
 
 static long asfreq_BtoD(long fromDate, char relation) {
@@ -312,42 +244,135 @@
 static long asfreq_BtoS(long fromDate, char relation) {
     return asfreq_DtoS(asfreq_BtoD(fromDate, relation), relation); }
 
-//************ FROM HOURLY ***************
+//************ FROM WEEKLY ***************
 
-static long asfreq_HtoA(long fromDate, char relation) { return 0; }
-static long asfreq_HtoQ(long fromDate, char relation) { return 0; }
-static long asfreq_HtoM(long fromDate, char relation) { return 0; }
-static long asfreq_HtoW(long fromDate, char relation) { return 0; }
-static long asfreq_HtoB(long fromDate, char relation) { return 0; }
-static long asfreq_HtoB_forConvert(long fromDate, char relation) { return 0; }
-static long asfreq_HtoD(long fromDate, char relation) { return 0; }
-static long asfreq_HtoT(long fromDate, char relation) { return 0; }
-static long asfreq_HtoS(long fromDate, char relation) { return 0; }
+static long asfreq_WtoA(long fromDate, char relation) { return -1; }
+static long asfreq_WtoQ(long fromDate, char relation) { return -1; }
+static long asfreq_WtoM(long fromDate, char relation) { return -1; }
+static long asfreq_WtoB(long fromDate, char relation) { return -1; }
+static long asfreq_WtoD(long fromDate, char relation) { return -1; }
+static long asfreq_WtoH(long fromDate, char relation) { return -1; }
+static long asfreq_WtoT(long fromDate, char relation) { return -1; }
+static long asfreq_WtoS(long fromDate, char relation) { return -1; }
 
-//************ FROM MINUTELY ***************
+//************ FROM MONTHLY ***************
 
-static long asfreq_TtoA(long fromDate, char relation) { return 0; }
-static long asfreq_TtoQ(long fromDate, char relation) { return 0; }
-static long asfreq_TtoM(long fromDate, char relation) { return 0; }
-static long asfreq_TtoW(long fromDate, char relation) { return 0; }
-static long asfreq_TtoB(long fromDate, char relation) { return 0; }
-static long asfreq_TtoB_forConvert(long fromDate, char relation) { return 0; }
-static long asfreq_TtoD(long fromDate, char relation) { return 0; }
-static long asfreq_TtoH(long fromDate, char relation) { return 0; }
-static long asfreq_TtoS(long fromDate, char relation) { return 0; }
+static long asfreq_MtoA(long fromDate, char relation) { return (fromDate - 1) / 12; }
+static long asfreq_MtoQ(long fromDate, char relation) { return (fromDate - 1) / 3 + 1; }
 
-//************ FROM SECONDLY ***************
+static long asfreq_MtoW(long fromDate, char relation) { return -1; }
 
-static long asfreq_StoA(long fromDate, char relation) { return 0; }
-static long asfreq_StoQ(long fromDate, char relation) { return 0; }
-static long asfreq_StoM(long fromDate, char relation) { return 0; }
-static long asfreq_StoW(long fromDate, char relation) { return 0; }
-static long asfreq_StoB(long fromDate, char relation) { return 0; }
-static long asfreq_StoB_forConvert(long fromDate, char relation) { return 0; }
-static long asfreq_StoD(long fromDate, char relation) { return 0; }
-static long asfreq_StoH(long fromDate, char relation) { return 0; }
-static long asfreq_StoT(long fromDate, char relation) { return 0; }
+static void MtoD_ym(long fromDate, long *y, long *m) {
+    *y = (fromDate - 1) / 12;
+    *m = fromDate  - 12 * (*y);
+}
 
+static long asfreq_MtoB(long fromDate, char relation) {
+
+    long y, m;
+
+    if (relation == 'B') {
+        MtoD_ym(fromDate, &y, &m);
+        return busday_before(y,m,1);
+    } else {
+        MtoD_ym(fromDate+1, &y, &m);
+        return busday_after(y, m, 1);
+    }
+}
+
+static long asfreq_MtoD(long fromDate, char relation) {
+
+    long y, m;
+
+    if (relation == 'B') {
+        MtoD_ym(fromDate, &y, &m);
+        return absdate_from_ymd(y, m, 1);
+    } else {
+        MtoD_ym(fromDate+1, &y, &m);
+        return absdate_from_ymd(y, m, 1) - 1;
+    }
+}
+
+static long asfreq_MtoH(long fromDate, char relation) { return asfreq_DtoH(asfreq_MtoD(fromDate, relation), relation); }
+static long asfreq_MtoT(long fromDate, char relation) { return asfreq_DtoT(asfreq_MtoD(fromDate, relation), relation); }
+static long asfreq_MtoS(long fromDate, char relation) { return asfreq_DtoS(asfreq_MtoD(fromDate, relation), relation); }
+
+//************ FROM QUARTERLY ***************
+
+static long asfreq_QtoA(long fromDate, char relation) { return (fromDate - 1) / 4; }
+
+static long asfreq_QtoM(long fromDate, char relation) {
+    if (relation == 'B') { return fromDate * 3 - 2; }
+    else {                 return fromDate  * 3; }
+}
+
+static long asfreq_QtoW(long fromDate, char relation) { return -1; }
+
+static void QtoD_ym(long fromDate, long *y, long *m) {
+    *y = (fromDate - 1) / 4;
+    *m = fromDate * 3 - 12 * (*y) - 2;
+}
+
+static long asfreq_QtoB(long fromDate, char relation) {
+
+    long y, m;
+
+    if (relation == 'B') {
+        QtoD_ym(fromDate, &y, &m);
+        return busday_before(y,m,1);
+    } else {
+        QtoD_ym(fromDate+1, &y, &m);
+        return busday_after(y, m, 1);
+    }
+}
+
+static long asfreq_QtoD(long fromDate, char relation) {
+
+    long y, m;
+
+    if (relation == 'B') {
+        QtoD_ym(fromDate, &y, &m);
+        return absdate_from_ymd(y, m, 1);
+    } else {
+        QtoD_ym(fromDate+1, &y, &m);
+        return absdate_from_ymd(y, m, 1) - 1;
+    }
+}
+
+static long asfreq_QtoH(long fromDate, char relation) { return asfreq_DtoH(asfreq_QtoD(fromDate, relation), relation); }
+static long asfreq_QtoT(long fromDate, char relation) { return asfreq_DtoT(asfreq_QtoD(fromDate, relation), relation); }
+static long asfreq_QtoS(long fromDate, char relation) { return asfreq_DtoS(asfreq_QtoD(fromDate, relation), relation); }
+
+
+//************ FROM ANNUAL ***************
+
+static long asfreq_AtoQ(long fromDate, char relation) {
+    if (relation == 'B') { return fromDate * 4 + 1; }
+    else {                 return (fromDate + 1) * 4; }
+}
+
+static long asfreq_AtoM(long fromDate, char relation) {
+    if (relation == 'B') { return fromDate * 12 + 1; }
+    else {                 return (fromDate + 1) * 12; }
+}
+
+static long asfreq_AtoW(long fromDate, char relation) { return -1; }
+
+static long asfreq_AtoB(long fromDate, char relation) {
+    if (relation == 'B') { return busday_before(fromDate,1,1); }
+    else {                 return busday_after(fromDate+1,1,1); }
+}
+
+static long asfreq_AtoD(long fromDate, char relation) {
+    if (relation == 'B') { return absdate_from_ymd(fromDate,1,1); }
+    else {                 return absdate_from_ymd(fromDate+1,1,1) - 1; }
+}
+
+static long asfreq_AtoH(long fromDate, char relation) { return asfreq_DtoH(asfreq_AtoD(fromDate, relation), relation); }
+static long asfreq_AtoT(long fromDate, char relation) { return asfreq_DtoT(asfreq_AtoD(fromDate, relation), relation); }
+static long asfreq_AtoS(long fromDate, char relation) { return asfreq_DtoS(asfreq_AtoD(fromDate, relation), relation); }
+
+
 static long nofunc(long fromDate, char relation) { return -1; }
 
 // end of frequency specific conversion routines
@@ -494,82 +519,11 @@
     }
 }
 
-static long dInfo_year(mxDateTimeObject *dateObj)    { return dateObj->year; }
-static long dInfo_quarter(mxDateTimeObject *dateObj) { return ((dateObj->month-1)/3)+1; }
-static long dInfo_month(mxDateTimeObject *dateObj)   { return dateObj->month; }
-static long dInfo_day(mxDateTimeObject *dateObj)     { return dateObj->day; }
-static long dInfo_dow(mxDateTimeObject *dateObj)     { return dateObj->day_of_week; }
-
-static char cseries_getDateInfo_doc[] = "";
-static PyObject *
-cseries_getDateInfo(PyObject *self, PyObject *args)
-{
-    char *freq;
-    char *info;
-
-    PyArrayObject *array;
-    PyArrayObject *newArray;
-    PyArrayIterObject *iterSource, *iterResult;
-    mxDateTimeObject *convDate;
-
-    PyObject *val;
-    long dateNum, dInfo;
-
-    long (*toDaily)(long, char) = NULL;
-    long (*getDateInfo)(mxDateTimeObject*) = NULL;
-
-    if (!PyArg_ParseTuple(args, "Oss:getDateInfo(array, freq, info)", &array, &freq, &info)) return NULL;
-    newArray = (PyArrayObject *)PyArray_Copy(array);
-
-    iterSource = (PyArrayIterObject *)PyArray_IterNew((PyObject *)array);
-    iterResult = (PyArrayIterObject *)PyArray_IterNew((PyObject *)newArray);
-
-    toDaily = get_asfreq_func(*freq, 'D', 0);
-
-    switch(*info)
-    {
-        case 'Y': //year
-            getDateInfo = &dInfo_year;
-            break;
-        case 'Q': //quarter
-            getDateInfo = &dInfo_quarter;
-            break;
-        case 'M': //month
-            getDateInfo = &dInfo_month;
-            break;
-        case 'D': //day
-            getDateInfo = &dInfo_day;
-            break;
-        case 'W': //day of week
-            getDateInfo = &dInfo_dow;
-            break;
-        default:
-            return NULL;
-    }
-
-    while (iterSource->index < iterSource->size) {
-
-        val = PyArray_GETITEM(array, iterSource->dataptr);
-        dateNum = PyInt_AsLong(val);
-
-        convDate = (mxDateTimeObject *)mxDateTime.DateTime_FromAbsDateAndTime(toDaily(dateNum, 'A'), 0);
-        dInfo = getDateInfo(convDate);
-        Py_DECREF(convDate);
-
-        PyArray_SETITEM(newArray, iterResult->dataptr, PyInt_FromLong(dInfo));
-
-        PyArray_ITER_NEXT(iterSource);
-        PyArray_ITER_NEXT(iterResult);
-    }
-
-    Py_DECREF(iterSource);
-    Py_DECREF(iterResult);
-
-    return (PyObject *) newArray;
-
-}
-
-
+/*
+Helper function for cseries_convert:
+    determine the size of the second dimension for the resulting
+    converted array
+*/
 static long get_height(char fromFreq, char toFreq) {
 
     int maxBusDaysPerYear, maxBusDaysPerQuarter, maxBusDaysPerMonth;
@@ -626,6 +580,7 @@
 static PyObject *
 cseries_convert(PyObject *self, PyObject *args)
 {
+    PyObject *arrayTest;
     PyArrayObject *array, *newArray;
     PyArrayObject *mask, *newMask;
 
@@ -685,7 +640,7 @@
 
     //convert end index to new frequency
     newEndTemp = asfreq_main(startIndex+array->dimensions[0]-1, 'A');
-    if (newEndTemp == -1) { newEnd = asfreq_endpoints(startIndex, 'B'); }
+    if (newEndTemp == -1) { newEnd = asfreq_endpoints(startIndex+array->dimensions[0]-1, 'B'); }
     else { newEnd = newEndTemp; }
 
     newLen = newEnd - newStart + 1;
@@ -698,16 +653,18 @@
 
         nd = 2;
         dim = PyDimMem_NEW(nd);
-        dim[0] = newLen;
-        dim[1] = newHeight;
+        dim[0] = (npy_intp)newLen;
+        dim[1] = (npy_intp)newHeight;
     } else {
         nd = 1;
         dim = PyDimMem_NEW(nd);
-        dim[0] = newLen;
+        dim[0] = (npy_intp)newLen;
     }
 
     newIdx = PyDimMem_NEW(nd);
-    newArray = (PyArrayObject*)PyArray_SimpleNew(nd, dim, array->descr->type_num);
+    arrayTest = PyArray_SimpleNew(nd, dim, array->descr->type_num);
+    if (arrayTest == NULL) { return NULL; }
+    newArray = (PyArrayObject*)arrayTest;
     newMask  = (PyArrayObject*)PyArray_SimpleNew(nd, dim, mask->descr->type_num);
 
     PyDimMem_FREE(dim);
@@ -805,7 +762,81 @@
 
 }
 
+static long dInfo_year(mxDateTimeObject *dateObj)    { return dateObj->year; }
+static long dInfo_quarter(mxDateTimeObject *dateObj) { return ((dateObj->month-1)/3)+1; }
+static long dInfo_month(mxDateTimeObject *dateObj)   { return dateObj->month; }
+static long dInfo_day(mxDateTimeObject *dateObj)     { return dateObj->day; }
+static long dInfo_dow(mxDateTimeObject *dateObj)     { return dateObj->day_of_week; }
 
+static char cseries_getDateInfo_doc[] = "";
+static PyObject *
+cseries_getDateInfo(PyObject *self, PyObject *args)
+{
+    char *freq;
+    char *info;
+
+    PyArrayObject *array;
+    PyArrayObject *newArray;
+    PyArrayIterObject *iterSource, *iterResult;
+    mxDateTimeObject *convDate;
+
+    PyObject *val;
+    long dateNum, dInfo;
+
+    long (*toDaily)(long, char) = NULL;
+    long (*getDateInfo)(mxDateTimeObject*) = NULL;
+
+    if (!PyArg_ParseTuple(args, "Oss:getDateInfo(array, freq, info)", &array, &freq, &info)) return NULL;
+    newArray = (PyArrayObject *)PyArray_Copy(array);
+
+    iterSource = (PyArrayIterObject *)PyArray_IterNew((PyObject *)array);
+    iterResult = (PyArrayIterObject *)PyArray_IterNew((PyObject *)newArray);
+
+    toDaily = get_asfreq_func(*freq, 'D', 0);
+
+    switch(*info)
+    {
+        case 'Y': //year
+            getDateInfo = &dInfo_year;
+            break;
+        case 'Q': //quarter
+            getDateInfo = &dInfo_quarter;
+            break;
+        case 'M': //month
+            getDateInfo = &dInfo_month;
+            break;
+        case 'D': //day
+            getDateInfo = &dInfo_day;
+            break;
+        case 'W': //day of week
+            getDateInfo = &dInfo_dow;
+            break;
+        default:
+            return NULL;
+    }
+
+    while (iterSource->index < iterSource->size) {
+
+        val = PyArray_GETITEM(array, iterSource->dataptr);
+        dateNum = PyInt_AsLong(val);
+
+        convDate = (mxDateTimeObject *)mxDateTime.DateTime_FromAbsDateAndTime(toDaily(dateNum, 'A'), 0);
+        dInfo = getDateInfo(convDate);
+        Py_DECREF(convDate);
+
+        PyArray_SETITEM(newArray, iterResult->dataptr, PyInt_FromLong(dInfo));
+
+        PyArray_ITER_NEXT(iterSource);
+        PyArray_ITER_NEXT(iterResult);
+    }
+
+    Py_DECREF(iterSource);
+    Py_DECREF(iterResult);
+
+    return (PyObject *) newArray;
+
+}
+
 ///////////////////////////////////////////////////////////////////////
 
 static PyMethodDef cseries_methods[] = {



More information about the Scipy-svn mailing list