Skip to content

Commit

Permalink
BUG: Unwanted conversion from timedelta to float (#18493) (#18586)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0e16818)
  • Loading branch information
fjdiod authored and TomAugspurger committed Dec 11, 2017
1 parent 5d6cc60 commit 04581ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.1.txt
Expand Up @@ -74,6 +74,7 @@ Indexing
- Bug where a ``MultiIndex`` with more than a million records was not raising ``AttributeError`` when trying to access a missing attribute (:issue:`18165`)
- Bug in :class:`IntervalIndex` constructor when a list of intervals is passed with non-default ``closed`` (:issue:`18334`)
- Bug in ``Index.putmask`` when an invalid mask passed (:issue:`18368`)
- Bug in masked assignment of a ``timedelta64[ns]`` dtype ``Series``, incorrectly coerced to float (:issue:`18493`)
-

I/O
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/internals.py
Expand Up @@ -1946,7 +1946,8 @@ def _can_hold_element(self, element):
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
return issubclass(tipo.type, np.timedelta64)
return isinstance(element, (timedelta, np.timedelta64))
return is_integer(element) or isinstance(
element, (timedelta, np.timedelta64))

def fillna(self, value, **kwargs):

Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexing/test_timedelta.py
Expand Up @@ -2,6 +2,7 @@

import pandas as pd
from pandas.util import testing as tm
import numpy as np


class TestTimedeltaIndexing(object):
Expand Down Expand Up @@ -47,3 +48,23 @@ def test_string_indexing(self):
expected = df.iloc[0]
sliced = df.loc['0 days']
tm.assert_series_equal(sliced, expected)

@pytest.mark.parametrize(
"value",
[None, pd.NaT, np.nan])
def test_masked_setitem(self, value):
# issue (#18586)
series = pd.Series([0, 1, 2], dtype='timedelta64[ns]')
series[series == series[0]] = value
expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]')
tm.assert_series_equal(series, expected)

@pytest.mark.parametrize(
"value",
[None, pd.NaT, np.nan])
def test_listlike_setitem(self, value):
# issue (#18586)
series = pd.Series([0, 1, 2], dtype='timedelta64[ns]')
series.iloc[0] = value
expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]')
tm.assert_series_equal(series, expected)

0 comments on commit 04581ae

Please sign in to comment.