diff options
-rw-r--r-- | doc/neps/datetime-proposal.rst | 21 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 8 | ||||
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 10 | ||||
-rw-r--r-- | numpy/core/src/multiarray/scalarapi.c | 36 |
5 files changed, 62 insertions, 15 deletions
diff --git a/doc/neps/datetime-proposal.rst b/doc/neps/datetime-proposal.rst index 8c8c42167..f72bab3ae 100644 --- a/doc/neps/datetime-proposal.rst +++ b/doc/neps/datetime-proposal.rst @@ -92,7 +92,7 @@ A limited number of divisions of any basic unit can be used to create multiples of a higher-resolution unit provided the divisor can be divided evenly into the number of higher-resolution units available. For example: Y/4 is just short-hand for -> (12M)/4 -> 3M and Y/4 will be -represented 1after creation as 3M The first lower unit found to have an +represented after creation as 3M. The first lower unit found to have an even divisor will be chosen (up to 3 lower units). The following standardized definitions are used in this specific case to find acceptable divisors @@ -112,7 +112,6 @@ m 60s, 60000ms s, ms, us, ns, ps, fs (use 1000 and 1000000 of the next two available lower units respectively). - Finally, a date-time data-type can be created with support for tracking sequential events within a basic unit: [D]//100, [Y]//4 (notice the required brackets). These ``modulo`` event units provide the following @@ -618,6 +617,24 @@ list of tuples describing a data-format can itself be a tuple of Final considerations ==================== +Why the fractional time and events: [3Y/12]//50 +----------------------------------------------- + +It is difficult to come up with enough units to satisfy every need. For +example, in C# on Windows the fundamental tick of time is 100ns. +Multiple of basic units are simple to handle. Divisors of basic units +are harder to handle arbitrarily, but it is common to mentally think of +a month as 1/12 of a year, or a day as 1/7 of a week. Therefore, the +ability to specify a unit in terms of a fraction of a "larger" unit was +implemented. + +The event notion (//50) was added to solve a use-case of a commercial +sponsor of this NEP. The idea is to allow timestamp to carry both event +number and timestamp information. The remainder carries the event +number information, while the quotient carries the timestamp +information. + + Why the ``origin`` metadata disappeared --------------------------------------- diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 0767d32ea..f9ddf8ce1 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -37,7 +37,7 @@ Exported symbols include: float_, complex_, longfloat, clongfloat, - datetime, timedelta, (these inherit from timeinteger which inherits from signedinteger) + datetime_, timedelta_, (these inherit from timeinteger which inherits from signedinteger) As part of the type-hierarchy: xx -- is bit-width @@ -373,13 +373,15 @@ def _set_up_aliases(): ('unicode_', 'unicode'), ('str_', 'string'), ('string_', 'string'), - ('object_', 'object')] + ('object_', 'object'), + ('timedelta_', 'timedelta'), + ('datetime_', 'datetime')] for alias, t in type_pairs: allTypes[alias] = allTypes[t] sctypeDict[alias] = sctypeDict[t] # Remove aliases overriding python types and modules for t in ['ulong', 'object', 'unicode', 'int', 'long', 'float', - 'complex', 'bool', 'string']: + 'complex', 'bool', 'string', 'datetime', 'timedelta']: try: del allTypes[t] del sctypeDict[t] diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 2cac3c04e..bffe9f332 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -3359,7 +3359,7 @@ set_typeinfo(PyObject *dict) (PyObject *)\ &Py@Name@ArrType_Type)); Py_DECREF(s); - /**end repeat**/ +/**end repeat**/ PyDict_SetItemString(infodict, "OBJECT", s=Py_BuildValue("ciiiO", PyArray_OBJECTLTR, diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index c2087a77b..664ed94c9 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -621,8 +621,6 @@ _convert_from_datetime_tuple(PyObject *obj) PyObject *dt_tuple; PyObject *dt_cobj; PyObject *datetime; - static PyObject *freq_key=NULL; - if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj)!=2) { PyErr_SetString(PyExc_RuntimeError, "_datetimestring is " \ @@ -649,12 +647,6 @@ _convert_from_datetime_tuple(PyObject *obj) if (new == NULL) return NULL; - /* Add correct metadata */ - if (freq_key == NULL) { - freq_key = PyString_InternFromString(NPY_METADATA_DTSTR); - if (freq_key == NULL) return NULL; - } - /* Remove any reference to old metadata dictionary */ /* And create a new one for this new dtype */ Py_XDECREF(new->metadata); @@ -667,7 +659,7 @@ _convert_from_datetime_tuple(PyObject *obj) } /* Assume this sets a new reference to dt_cobj */ - PyDict_SetItem(new->metadata, freq_key, dt_cobj); + PyDict_SetItemString(new->metadata, NPY_METADATA_DTSTR, dt_cobj); Py_DECREF(dt_cobj); return new; diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c index 225850bb7..c723533e5 100644 --- a/numpy/core/src/multiarray/scalarapi.c +++ b/numpy/core/src/multiarray/scalarapi.c @@ -282,6 +282,7 @@ PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode) Py_INCREF(scalar); return r; } + r = PyArray_NewFromDescr(&PyArray_Type, typecode, 0, NULL, @@ -510,6 +511,41 @@ PyArray_DescrFromScalar(PyObject *sc) Py_INCREF(descr); return descr; } + + if (PyArray_IsScalar(sc, TimeInteger)) { + PyObject *cobj; + PyArray_DatetimeMetaData *dt_data; + + dt_data = _pya_malloc(sizeof(PyArray_DatetimeMetaData)); + + if (PyArray_IsScalar(sc, Datetime)) { + descr = PyArray_DescrNewFromType(PyArray_DATETIME); + memcpy(dt_data, &((PyDatetimeScalarObject *)sc)->obmeta, + sizeof(PyArray_DatetimeMetaData)); + } + else {/* Timedelta */ + descr = PyArray_DescrNewFromType(PyArray_TIMEDELTA); + memcpy(dt_data, &((PyTimedeltaScalarObject *)sc)->obmeta, + sizeof(PyArray_DatetimeMetaData)); + } + cobj = PyCObject_FromVoidPtr((void *)dt_data, _pya_free); + + /* Add correct meta-data to the data-type */ + + if (descr == NULL) {Py_DECREF(cobj); return NULL;} + + Py_XDECREF(descr->metadata); + if ((descr->metadata = PyDict_New())== NULL) { + Py_DECREF(descr); Py_DECREF(cobj); return NULL; + } + + /* Assume this sets a new reference to cobj */ + PyDict_SetItemString(descr->metadata, NPY_METADATA_DTSTR, cobj); + Py_DECREF(cobj); + + return descr; + } + descr = PyArray_DescrFromTypeObject((PyObject *)sc->ob_type); if (descr->elsize == 0) { PyArray_DESCR_REPLACE(descr); |