Skip to content

Commit 0e16818

Browse files
fjdiodjreback
authored andcommitted
BUG: Unwanted conversion from timedelta to float (#18493) (#18586)
1 parent e1ba19a commit 0e16818

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Indexing
7474
- Bug where a ``MultiIndex`` with more than a million records was not raising ``AttributeError`` when trying to access a missing attribute (:issue:`18165`)
7575
- Bug in :class:`IntervalIndex` constructor when a list of intervals is passed with non-default ``closed`` (:issue:`18334`)
7676
- Bug in ``Index.putmask`` when an invalid mask passed (:issue:`18368`)
77+
- Bug in masked assignment of a ``timedelta64[ns]`` dtype ``Series``, incorrectly coerced to float (:issue:`18493`)
7778
-
7879

7980
I/O

pandas/core/internals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,8 @@ def _can_hold_element(self, element):
19561956
tipo = maybe_infer_dtype_type(element)
19571957
if tipo is not None:
19581958
return issubclass(tipo.type, np.timedelta64)
1959-
return isinstance(element, (timedelta, np.timedelta64))
1959+
return is_integer(element) or isinstance(
1960+
element, (timedelta, np.timedelta64))
19601961

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

pandas/tests/indexing/test_timedelta.py

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pandas as pd
44
from pandas.util import testing as tm
5+
import numpy as np
56

67

78
class TestTimedeltaIndexing(object):
@@ -47,3 +48,23 @@ def test_string_indexing(self):
4748
expected = df.iloc[0]
4849
sliced = df.loc['0 days']
4950
tm.assert_series_equal(sliced, expected)
51+
52+
@pytest.mark.parametrize(
53+
"value",
54+
[None, pd.NaT, np.nan])
55+
def test_masked_setitem(self, value):
56+
# issue (#18586)
57+
series = pd.Series([0, 1, 2], dtype='timedelta64[ns]')
58+
series[series == series[0]] = value
59+
expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]')
60+
tm.assert_series_equal(series, expected)
61+
62+
@pytest.mark.parametrize(
63+
"value",
64+
[None, pd.NaT, np.nan])
65+
def test_listlike_setitem(self, value):
66+
# issue (#18586)
67+
series = pd.Series([0, 1, 2], dtype='timedelta64[ns]')
68+
series.iloc[0] = value
69+
expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]')
70+
tm.assert_series_equal(series, expected)

0 commit comments

Comments
 (0)