8000 Merge pull request #5455 from gerritholl/masked_structured_datetime64 · numpy/numpy@1d87101 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d87101

Browse files
committed
Merge pull request #5455 from gerritholl/masked_structured_datetime64
BUG: Fix #4476 by adding datetime64 and timedelta64 types
2 parents 1444550 + 00ee332 commit 1d87101

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

numpy/ma/core.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,15 @@ class MaskError(MAError):
145145
'S' : 'N/A',
146146
'u' : 999999,
147147
'V' : '???',
148-
'U' : 'N/A',
149-
'M8[D]' : np.datetime64('NaT', 'D'),
150-
'M8[us]' : np.datetime64('NaT', 'us')
148+
'U' : 'N/A'
151149
}
150+
151+
# Add datetime64 and timedelta64 types
152+
for v in ["Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps",
153+
"fs", "as"]:
154+
default_filler["M8[" + v + "]"] = np.datetime64("NaT", v)
155+
default_filler["m8[" + v + "]"] = np.timedelta64("NaT", v)
156+
152157
max_filler = ntypes._minvals
153158
max_filler.update([(k, -np.inf) for k in [np.float32, np.float64]])
154159
min_filler = ntypes._maxvals
@@ -194,7 +199,7 @@ def default_fill_value(obj):
194199
999999
195200
>>> np.ma.default_fill_value(np.array([1.1, 2., np.pi]))
196201
1e+20
197-
>>> np.ma.default_fill_value(np.dtype(complex))
202+
>>> np.ma.default_fill_value(np.dtype(complex))
198203
(1e+20+0j)
199204
200205
"""
@@ -203,7 +208,7 @@ def default_fill_value(obj):
203208
elif isinstance(obj, np.dtype):
204209
if obj.subdtype:
205210
defval = default_filler.get(obj.subdtype[0].kind, '?')
206-
elif obj.kind == 'M':
211+
elif obj.kind in 'Mm':
207212
defval = default_filler.get(obj.str[1:], '?')
208213
else:
209214
defval = default_filler.get(obj.kind, '?')

numpy/ma/tests/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,21 @@ def test_fillvalue_exotic_dtype(self):
14891489
control = np.array((0, 0, 0), dtype="int, float, float").astype(ndtype)
14901490
assert_equal(_check_fill_value(0, ndtype), control)
14911491

1492+
def test_fillvalue_datetime_timedelta(self):
1493+
# Test default fillvalue for datetime64 and timedelta64 types.
1494+
# See issue #4476, this would return '?' which would cause errors
1495+
# elsewhere
1496+
1497+
for timecode in ("as", "fs", "ps", "ns", "us", "ms", "s", "m",
1498+
"h", "D", "W", "M", "Y"):
1499+
control = numpy.datetime64("NaT", timecode)
1500+
test = default_fill_value(numpy.dtype("<M8[" + timecode + "]"))
1501+
assert_equal(test, control)
1502+
1503+
control = numpy.timedelta64("NaT", timecode)
1504+
test = default_fill_value(numpy.dtype("<m8[" + timecode + "]"))
1505+
assert_equal(test, control)
1506+
14921507
def test_extremum_fill_value(self):
14931508
# Tests extremum fill values for flexible type.
14941509
a = array([(1, (2, 3)), (4, (5, 6))],

0 commit comments

Comments
 (0)
0