Skip to content

Commit 3112c7b

Browse files
authored
DEPR: Series[td64].fillna(incompat) cast instead of raise (#45746)
1 parent 2bb20bb commit 3112c7b

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Other Deprecations
193193
- Deprecated :meth:`DataFrame.iteritems`, :meth:`Series.iteritems`, :meth:`HDFStore.iteritems` in favor of :meth:`DataFrame.items`, :meth:`Series.items`, :meth:`HDFStore.items` (:issue:`45321`)
194194
- Deprecated :meth:`Series.is_monotonic` and :meth:`Index.is_monotonic` in favor of :meth:`Series.is_monotonic_increasing` and :meth:`Index.is_monotonic_increasing` (:issue:`45422`, :issue:`21335`)
195195
- Deprecated the ``__array_wrap__`` method of DataFrame and Series, rely on standard numpy ufuncs instead (:issue:`45451`)
196+
- Deprecated the behavior of :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtype and incompatible fill value; in a future version this will cast to a common dtype (usually object) instead of raising, matching the behavior of other dtypes (:issue:`45746`)
196197
-
197198

198199

pandas/core/internals/blocks.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,16 @@ def fillna(
14881488
# We support filling a DatetimeTZ with a `value` whose timezone
14891489
# is different by coercing to object.
14901490
if self.dtype.kind == "m":
1491-
# TODO: don't special-case td64
1491+
# GH#45746
1492+
warnings.warn(
1493+
"The behavior of fillna with timedelta64[ns] dtype and "
1494+
f"an incompatible value ({type(value)}) is deprecated. "
1495+
"In a future version, this will cast to a common dtype "
1496+
"(usually object) instead of raising, matching the "
1497+
"behavior of other dtypes.",
1498+
FutureWarning,
1499+
stacklevel=find_stack_level(),
1500+
)
14921501
raise
14931502
blk = self.coerce_to_target_dtype(value)
14941503
return blk.fillna(value, limit, inplace, downcast)

pandas/tests/series/methods/test_fillna.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,11 @@ def test_timedelta_fillna(self, frame_or_series):
250250

251251
# interpreted as seconds, no longer supported
252252
msg = "value should be a 'Timedelta', 'NaT', or array of those. Got 'int'"
253+
wmsg = "In a future version, this will cast to a common dtype"
253254
with pytest.raises(TypeError, match=msg):
254-
obj.fillna(1)
255+
with tm.assert_produces_warning(FutureWarning, match=wmsg):
256+
# GH#45746
257+
obj.fillna(1)
255258

256259
result = obj.fillna(Timedelta(seconds=1))
257260
expected = Series(

0 commit comments

Comments
 (0)