Skip to content

Commit 15df8a4

Browse files
jbrockmendeljreback
authored andcommitted
BUG: appending a Timedelta to Series incorrectly casts to integer (pandas-dev#27303)
1 parent 50fb400 commit 15df8a4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ Indexing
10501050
- Bug in :class:`Categorical` and :class:`CategoricalIndex` with :class:`Interval` values when using the ``in`` operator (``__contains``) with objects that are not comparable to the values in the ``Interval`` (:issue:`23705`)
10511051
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` on a :class:`DataFrame` with a single timezone-aware datetime64[ns] column incorrectly returning a scalar instead of a :class:`Series` (:issue:`27110`)
10521052
- Bug in :class:`CategoricalIndex` and :class:`Categorical` incorrectly raising ``ValueError`` instead of ``TypeError`` when a list is passed using the ``in`` operator (``__contains__``) (:issue:`21729`)
1053+
- Bug in setting a new value in a :class:`Series` with a :class:`Timedelta` object incorrectly casting the value to an integer (:issue:`22717`)
10531054
-
10541055
10551056
Missing

pandas/core/indexing.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
is_sequence,
2121
is_sparse,
2222
)
23+
from pandas.core.dtypes.concat import _concat_compat
2324
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
2425
from pandas.core.dtypes.missing import _infer_fill_value, isna
2526

@@ -432,11 +433,9 @@ def _setitem_with_indexer(self, indexer, value):
432433
# this preserves dtype of the value
433434
new_values = Series([value])._values
434435
if len(self.obj._values):
435-
try:
436-
new_values = np.concatenate([self.obj._values, new_values])
437-
except TypeError:
438-
as_obj = self.obj.astype(object)
439-
new_values = np.concatenate([as_obj, new_values])
436+
# GH#22717 handle casting compatibility that np.concatenate
437+
# does incorrectly
438+
new_values = _concat_compat([self.obj._values, new_values])
440439
self.obj._data = self.obj._constructor(
441440
new_values, index=new_index, name=self.obj.name
442441
)._data

pandas/tests/series/indexing/test_indexing.py

+23
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,29 @@ def test_timedelta_assignment():
654654
tm.assert_series_equal(s, expected)
655655

656656

657+
@pytest.mark.parametrize(
658+
"td",
659+
[
660+
pd.Timedelta("9 days"),
661+
pd.Timedelta("9 days").to_timedelta64(),
662+
pd.Timedelta("9 days").to_pytimedelta(),
663+
],
664+
)
665+
def test_append_timedelta_does_not_cast(td):
666+
# GH#22717 inserting a Timedelta should _not_ cast to int64
667+
expected = pd.Series(["x", td], index=[0, "td"], dtype=object)
668+
669+
ser = pd.Series(["x"])
670+
ser["td"] = td
671+
tm.assert_series_equal(ser, expected)
672+
assert isinstance(ser["td"], pd.Timedelta)
673+
674+
ser = pd.Series(["x"])
675+
ser.loc["td"] = pd.Timedelta("9 days")
676+
tm.assert_series_equal(ser, expected)
677+
assert isinstance(ser["td"], pd.Timedelta)
678+
679+
657680
def test_underlying_data_conversion():
658681
# GH 4080
659682
df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]})

0 commit comments

Comments
 (0)