Skip to content

Commit 2ae5152

Browse files
jbrockmendeljreback
authored andcommitted
BUG: fix+test fillna with non-nano datetime64; closes #27419 (#27425)
1 parent 8ffdf7a commit 2ae5152

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/core/internals/blocks.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,12 @@ def _astype(self, dtype, **kwargs):
22502250
def _can_hold_element(self, element):
22512251
tipo = maybe_infer_dtype_type(element)
22522252
if tipo is not None:
2253-
return is_dtype_equal(tipo, self.dtype)
2253+
if self.is_datetimetz:
2254+
# require exact match, since non-nano does not exist
2255+
return is_dtype_equal(tipo, self.dtype)
2256+
2257+
# GH#27419 if we get a non-nano datetime64 object
2258+
return is_datetime64_dtype(tipo)
22542259
elif element is NaT:
22552260
return True
22562261
elif isinstance(element, datetime):

pandas/tests/series/test_missing.py

+11
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,17 @@ def test_datetime64_tz_fillna(self):
437437
)
438438
assert_series_equal(df.fillna(method="bfill"), exp)
439439

440+
def test_datetime64_non_nano_fillna(self):
441+
# GH#27419
442+
ser = Series([Timestamp("2010-01-01"), pd.NaT, Timestamp("2000-01-01")])
443+
val = np.datetime64("1975-04-05", "ms")
444+
445+
result = ser.fillna(val)
446+
expected = Series(
447+
[Timestamp("2010-01-01"), Timestamp("1975-04-05"), Timestamp("2000-01-01")]
448+
)
449+
tm.assert_series_equal(result, expected)
450+
440451
def test_fillna_consistency(self):
441452
# GH 16402
442453
# fillna with a tz aware to a tz-naive, should result in object

0 commit comments

Comments
 (0)