Skip to content

Commit bad02e8

Browse files
jbrockmendeljreback
authored andcommitted
fix interpreting int as second--> nano (#24694)
1 parent 4bd286a commit bad02e8

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,7 @@ Deprecations
13091309
- In :meth:`Series.where` with Categorical data, providing an ``other`` that is not present in the categories is deprecated. Convert the categorical to a different dtype or add the ``other`` to the categories first (:issue:`24077`).
13101310
- :meth:`Series.clip_lower`, :meth:`Series.clip_upper`, :meth:`DataFrame.clip_lower` and :meth:`DataFrame.clip_upper` are deprecated and will be removed in a future version. Use ``Series.clip(lower=threshold)``, ``Series.clip(upper=threshold)`` and the equivalent ``DataFrame`` methods (:issue:`24203`)
13111311
- :meth:`Series.nonzero` is deprecated and will be removed in a future version (:issue:`18262`)
1312+
- Passing an integer to :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtypes is deprecated, will raise ``TypeError`` in a future version. Use ``obj.fillna(pd.Timedelta(...))` instead (:issue:`24694`)
13121313
13131314
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
13141315

pandas/core/internals/blocks.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2499,8 +2499,14 @@ def _can_hold_element(self, element):
24992499
def fillna(self, value, **kwargs):
25002500

25012501
# allow filling with integers to be
2502-
# interpreted as seconds
2502+
# interpreted as nanoseconds
25032503
if is_integer(value) and not isinstance(value, np.timedelta64):
2504+
# Deprecation GH#24694, GH#19233
2505+
warnings.warn("Passing integers to fillna is deprecated, will "
2506+
"raise a TypeError in a future version. To retain "
2507+
"the old behavior, pass pd.Timedelta(seconds=n) "
2508+
"instead.",
2509+
FutureWarning, stacklevel=6)
25042510
value = Timedelta(value, unit='s')
25052511
return super(TimeDeltaBlock, self).fillna(value, **kwargs)
25062512

pandas/tests/series/test_missing.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ def test_timedelta_fillna(self):
6565
td = s.diff()
6666

6767
# reg fillna
68-
result = td.fillna(0)
68+
with tm.assert_produces_warning(FutureWarning):
69+
result = td.fillna(0)
6970
expected = Series([timedelta(0), timedelta(0), timedelta(1),
7071
timedelta(days=1, seconds=9 * 3600 + 60 + 1)])
7172
assert_series_equal(result, expected)
7273

73-
# interprested as seconds
74-
result = td.fillna(1)
75-
expected = Series([timedelta(seconds=1), timedelta(0), timedelta(1),
74+
# interpreted as seconds, deprecated
75+
with tm.assert_produces_warning(FutureWarning):
76+
result = td.fillna(1)
77+
expected = Series([timedelta(seconds=1),
78+
timedelta(0), timedelta(1),
7679
timedelta(days=1, seconds=9 * 3600 + 60 + 1)])
7780
assert_series_equal(result, expected)
7881

@@ -96,14 +99,16 @@ def test_timedelta_fillna(self):
9699
# ffill
97100
td[2] = np.nan
98101
result = td.ffill()
99-
expected = td.fillna(0)
102+
with tm.assert_produces_warning(FutureWarning):
103+
expected = td.fillna(0)
100104
expected[0] = np.nan
101105
assert_series_equal(result, expected)
102106

103107
# bfill
104108
td[2] = np.nan
105109
result = td.bfill()
106-
expected = td.fillna(0)
110+
with tm.assert_produces_warning(FutureWarning):
111+
expected = td.fillna(0)
107112
expected[2] = timedelta(days=1, seconds=9 * 3600 + 60 + 1)
108113
assert_series_equal(result, expected)
109114

0 commit comments

Comments
 (0)