diff options
author | Travis Oliphant <oliphant@enthought.com> | 2009-11-19 16:38:09 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2009-11-19 16:38:09 +0000 |
commit | b0e32f4064ecee2b66d787cb4b5a7bb49f4627c4 (patch) | |
tree | cecfa531192d74c17364cf62406f31eca95aa3bc | |
parent | 0edcfffdcddf675f0ab8b8f2f764dbdbe914adfb (diff) | |
download | numpy-b0e32f4064ecee2b66d787cb4b5a7bb49f4627c4.tar.gz |
Add comparison of datetime dtype objects that compares units correctly and add new metadata for new datetime dtypes.
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 11 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 32 |
2 files changed, 36 insertions, 7 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index ad701bdd1..202ba2c38 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -652,13 +652,10 @@ _convert_from_datetime_tuple(PyObject *obj) if (freq_key == NULL) return NULL; } - if (new->metadata == NULL) { - if ((new->metadata = PyDict_New())== NULL) return NULL; - } - if (!PyDict_Check(new->metadata)) { - PyErr_SetString(PyExc_RuntimeError, "metadata is not a dictionary"); - return NULL; - } + /* Remove any reference to old metadata dictionary */ + /* And create a new one for this new dtype */ + Py_XDECREF(new->metadata); + if ((new->metadata = PyDict_New())== NULL) return NULL; dt_cobj = _convert_datetime_tuple_to_cobj(dt_tuple); if (dt_cobj == NULL) { /* Failure in conversion */ diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 8573c48ab..9244ba446 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1307,6 +1307,33 @@ _equivalent_fields(PyObject *field1, PyObject *field2) { return same; } +/* + * compare the metadata for two date-times + * return 1 if they are the same + * or 0 if not + */ +static int +_equivalent_units(PyObject *meta1, PyObject *meta2) +{ + PyObject *cobj1, *cobj2; + PyArray_DatetimeMetaData *data1, *data2; + + /* Same meta object */ + if (meta1 == meta2) + return 1; + + cobj1 = PyDict_GetItemString(meta1, NPY_METADATA_DTSTR); + cobj2 = PyDict_GetItemString(meta2, NPY_METADATA_DTSTR); + if (cobj1 == cobj2) + return 1; + + data1 = PyCObject_AsVoidPtr(cobj1); + data2 = PyCObject_AsVoidPtr(cobj2); + + return ((data1->base == data2->base) && (data1->num == data2->num) + && (data1->den == data2->den) && (data1->events == data2->events)); +} + /*NUMPY_API * @@ -1332,6 +1359,11 @@ PyArray_EquivTypes(PyArray_Descr *typ1, PyArray_Descr *typ2) return ((typenum1 == typenum2) && _equivalent_fields(typ1->fields, typ2->fields)); } + if (typenum1 == PyArray_DATETIME || typenum1 == PyArray_DATETIME + || typenum2 == PyArray_TIMEDELTA || typenum2 == PyArray_TIMEDELTA) { + return ((typenum1 == typenum2) && + _equivalent_units(typ1->metadata, typ2->metadata)); + } return (typ1->kind == typ2->kind); } |