Skip to content

Commit f0877ec

Browse files
icexellossjreback
authored andcommitted
BUG: Avoid casting to double type unnecessarily when setting values i… (#23462)
1 parent 19baa61 commit f0877ec

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ Indexing
12101210
- :class:`Index` no longer mangles ``None``, ``NaN`` and ``NaT``, i.e. they are treated as three different keys. However, for numeric Index all three are still coerced to a ``NaN`` (:issue:`22332`)
12111211
- Bug in `scalar in Index` if scalar is a float while the ``Index`` is of integer dtype (:issue:`22085`)
12121212
- Bug in `MultiIndex.set_levels` when levels value is not subscriptable (:issue:`23273`)
1213+
- Bug where setting a timedelta column by ``Index`` causes it to be casted to double, and therefore lose precision (:issue:`23511`)
12131214

12141215
Missing
12151216
^^^^^^^

pandas/core/internals/blocks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2104,9 +2104,9 @@ def _box_func(self):
21042104
def _can_hold_element(self, element):
21052105
tipo = maybe_infer_dtype_type(element)
21062106
if tipo is not None:
2107-
return issubclass(tipo.type, np.timedelta64)
2107+
return issubclass(tipo.type, (np.timedelta64, np.int64))
21082108
return is_integer(element) or isinstance(
2109-
element, (timedelta, np.timedelta64))
2109+
element, (timedelta, np.timedelta64, np.int64))
21102110

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

pandas/tests/indexing/test_timedelta.py

+15
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,18 @@ def test_numpy_timedelta_scalar_indexing(self, start, stop,
8080
result = s.loc[slice(start, stop)]
8181
expected = s.iloc[expected_slice]
8282
tm.assert_series_equal(result, expected)
83+
84+
def test_roundtrip_thru_setitem(self):
85+
# PR 23462
86+
dt1 = pd.Timedelta(0)
87+
dt2 = pd.Timedelta(28767471428571405)
88+
df = pd.DataFrame({'dt': pd.Series([dt1, dt2])})
89+
df_copy = df.copy()
90+
s = pd.Series([dt1])
91+
92+
expected = df['dt'].iloc[1].value
93+
df.loc[[True, False]] = s
94+
result = df['dt'].iloc[1].value
95+
96+
assert expected == result
97+
tm.assert_frame_equal(df, df_copy)

0 commit comments

Comments
 (0)