From 4bfcf4801ceedd0563e3648154fc49985729c442 Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Thu, 15 Jan 2015 15:32:28 -0500 Subject: [PATCH 1/2] BUG: Fix #4476 by adding datetime64 and timedelta64 types This commit fixes bug #4476 by adding the codes for the datetime64 and timedelta64 types to the `default_filler` dictionary in numpy.ma.core, used by `default_fill_value`. Also adapt checking in the `default_fill_value` to include code for timedelta64, not only datetime64. --- numpy/ma/core.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index bbaaaa3f17cb..5d878363d206 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -145,10 +145,15 @@ class MaskError(MAError): 'S' : 'N/A', 'u' : 999999, 'V' : '???', - 'U' : 'N/A', - 'M8[D]' : np.datetime64('NaT', 'D'), - 'M8[us]' : np.datetime64('NaT', 'us') + 'U' : 'N/A' } + +# Add datetime64 and timedelta64 types +for v in ["Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps", + "fs", "as"]: + default_filler["M8[" + v + "]"] = np.datetime64("NaT", v) + default_filler["m8[" + v + "]"] = np.timedelta64("NaT", v) + max_filler = ntypes._minvals max_filler.update([(k, -np.inf) for k in [np.float32, np.float64]]) min_filler = ntypes._maxvals @@ -194,7 +199,7 @@ def default_fill_value(obj): 999999 >>> np.ma.default_fill_value(np.array([1.1, 2., np.pi])) 1e+20 - >>> np.ma.default_fill_value(np.dtype(complex)) + >>> np.ma.default_fill_value(np.dtype(complex)) (1e+20+0j) """ @@ -203,7 +208,7 @@ def default_fill_value(obj): elif isinstance(obj, np.dtype): if obj.subdtype: defval = default_filler.get(obj.subdtype[0].kind, '?') - elif obj.kind == 'M': + elif obj.kind in 'Mm': defval = default_filler.get(obj.str[1:], '?') else: defval = default_filler.get(obj.kind, '?') From 00ee332e9d95362a3d487f784cdc8fc06cf0832c Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Thu, 15 Jan 2015 15:33:18 -0500 Subject: [PATCH 2/2] TST: Add testcase for the fix to bug #4476. Add a testcase `test_fillvalue_datetime_timedelta` to class `TestFillingValues` for the fix to bug #4476. See commit 216fd17 and pull request #5455. --- numpy/ma/tests/test_core.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index a2ecccb40194..975b7acebaea 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1489,6 +1489,21 @@ def test_fillvalue_exotic_dtype(self): control = np.array((0, 0, 0), dtype="int, float, float").astype(ndtype) assert_equal(_check_fill_value(0, ndtype), control) + def test_fillvalue_datetime_timedelta(self): + # Test default fillvalue for datetime64 and timedelta64 types. + # See issue #4476, this would return '?' which would cause errors + # elsewhere + + for timecode in ("as", "fs", "ps", "ns", "us", "ms", "s", "m", + "h", "D", "W", "M", "Y"): + control = numpy.datetime64("NaT", timecode) + test = default_fill_value(numpy.dtype("