Skip to content

Commit

Permalink
fix interpreting int as second--> nano (#24694)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jan 10, 2019
1 parent 4bd286a commit bad02e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 11 additions & 6 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit bad02e8

Please sign in to comment.