diff options
author | Mark Wiebe <mwiebe@enthought.com> | 2011-05-19 09:31:52 -0500 |
---|---|---|
committer | Mark Wiebe <mwiebe@enthought.com> | 2011-05-19 17:13:50 -0500 |
commit | 25bcc658a2031d709a052cdf62c8329a59b76b20 (patch) | |
tree | 5d78044a4af55a6c232473fe46b0899771b3ea3c | |
parent | 026bc920296f8f3fbab22697a92dec8b844de59f (diff) | |
download | numpy-25bcc658a2031d709a052cdf62c8329a59b76b20.tar.gz |
ENH: Small changes towards datetime type promotion
-rw-r--r-- | numpy/core/src/multiarray/convert_datatype.c | 45 | ||||
-rw-r--r-- | numpy/core/src/multiarray/scalartypes.c.src | 5 |
2 files changed, 42 insertions, 8 deletions
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index ee22ba646..0439c42fc 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -542,7 +542,10 @@ PyArray_PromoteTypes(PyArray_Descr *type1, PyArray_Descr *type2) /* If they're built-in types, use the promotion table */ if (type_num1 < NPY_NTYPES && type_num2 < NPY_NTYPES) { ret_type_num = _npy_type_promotion_table[type_num1][type_num2]; - /* The table doesn't handle string/unicode/void, check the result */ + /* + * The table doesn't handle string/unicode/void/datetime/timedelta, + * so check the result + */ if (ret_type_num >= 0) { return PyArray_DescrFromType(ret_type_num); } @@ -647,10 +650,16 @@ PyArray_PromoteTypes(PyArray_Descr *type1, PyArray_Descr *type2) } switch (type_num1) { - /* BOOL can convert to anything */ + /* BOOL can convert to anything except datetime/timedelta/void */ case NPY_BOOL: - Py_INCREF(type2); - return type2; + if (type_num2 != NPY_DATETIME && type_num2 != NPY_TIMEDELTA && + type_num2 != NPY_VOID) { + Py_INCREF(type2); + return type2; + } + else { + break; + } /* For strings and unicodes, take the larger size */ case NPY_STRING: if (type_num2 == NPY_STRING) { @@ -713,13 +722,37 @@ PyArray_PromoteTypes(PyArray_Descr *type1, PyArray_Descr *type2) return type1; } break; + case NPY_DATETIME: + if (type_num2 == NPY_DATETIME) { + } + /* 'M[A]','m[B]' -> 'M[A]' */ + else if (type_num2 == NPY_TIMEDELTA) { + Py_INCREF(type1); + return type1; + } + break; + case NPY_TIMEDELTA: + if (type_num2 == NPY_DATETIME) { + } + else if (type_num2 == NPY_TIMEDELTA) { + } + else if (PyTypeNum_ISINTEGER(type_num2) || + PyTypeNum_ISFLOAT(type_num2)) { + } + break; } switch (type_num2) { /* BOOL can convert to anything */ case NPY_BOOL: - Py_INCREF(type1); - return type1; + if (type_num1 != NPY_DATETIME && type_num1 != NPY_TIMEDELTA && + type_num1 != NPY_VOID) { + Py_INCREF(type1); + return type1; + } + else { + break; + } case NPY_STRING: /* Allow NUMBER -> STRING */ if (PyTypeNum_ISNUMBER(type_num1)) { diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 58dc39861..ebcfd4f96 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -3576,8 +3576,9 @@ initialize_casting_tables(void) */ for (i = 0; i < NPY_NTYPES; ++i) { _npy_type_promotion_table[i][i] = i; - /* Don't let number promote to string/unicode/void */ - if (i == NPY_STRING || i == NPY_UNICODE || i == NPY_VOID) { + /* Don't let number promote to string/unicode/void/datetime/timedelta */ + if (i == NPY_STRING || i == NPY_UNICODE || i == NPY_VOID || + i == NPY_DATETIME || i == NPY_TIMEDELTA) { /* Promoting these types requires examining their contents */ _npy_type_promotion_table[i][i] = -1; for (j = i+1; j < NPY_NTYPES; ++j) { |