From bad02e8875e11e03d438d66e38785203549a0b87 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 10 Jan 2019 09:42:46 -0800 Subject: [PATCH] fix interpreting int as second--> nano (#24694) --- doc/source/whatsnew/v0.24.0.rst | 1 + pandas/core/internals/blocks.py | 8 +++++++- pandas/tests/series/test_missing.py | 17 +++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index aee3d78243d2e..960b205c49c61 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1309,6 +1309,7 @@ Deprecations - 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`). - :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`) - :meth:`Series.nonzero` is deprecated and will be removed in a future version (:issue:`18262`) +- 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`) .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index e7c851c256081..8f9bc7b4adee8 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2499,8 +2499,14 @@ def _can_hold_element(self, element): def fillna(self, value, **kwargs): # allow filling with integers to be - # interpreted as seconds + # interpreted as nanoseconds if is_integer(value) and not isinstance(value, np.timedelta64): + # Deprecation GH#24694, GH#19233 + warnings.warn("Passing integers to fillna is deprecated, will " + "raise a TypeError in a future version. To retain " + "the old behavior, pass pd.Timedelta(seconds=n) " + "instead.", + FutureWarning, stacklevel=6) value = Timedelta(value, unit='s') return super(TimeDeltaBlock, self).fillna(value, **kwargs) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 90ef465c5f239..f4f16ff2d3ac1 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -65,14 +65,17 @@ def test_timedelta_fillna(self): td = s.diff() # reg fillna - result = td.fillna(0) + with tm.assert_produces_warning(FutureWarning): + result = td.fillna(0) expected = Series([timedelta(0), timedelta(0), timedelta(1), timedelta(days=1, seconds=9 * 3600 + 60 + 1)]) assert_series_equal(result, expected) - # interprested as seconds - result = td.fillna(1) - expected = Series([timedelta(seconds=1), timedelta(0), timedelta(1), + # interpreted as seconds, deprecated + with tm.assert_produces_warning(FutureWarning): + result = td.fillna(1) + expected = Series([timedelta(seconds=1), + timedelta(0), timedelta(1), timedelta(days=1, seconds=9 * 3600 + 60 + 1)]) assert_series_equal(result, expected) @@ -96,14 +99,16 @@ def test_timedelta_fillna(self): # ffill td[2] = np.nan result = td.ffill() - expected = td.fillna(0) + with tm.assert_produces_warning(FutureWarning): + expected = td.fillna(0) expected[0] = np.nan assert_series_equal(result, expected) # bfill td[2] = np.nan result = td.bfill() - expected = td.fillna(0) + with tm.assert_produces_warning(FutureWarning): + expected = td.fillna(0) expected[2] = timedelta(days=1, seconds=9 * 3600 + 60 + 1) assert_series_equal(result, expected)