diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 6ace245a4bae1..0e1eb1a4e842b 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -1118,6 +1118,7 @@ Datetimelike - Bug in :func:`DataFrame.combine` with datetimelike values raising a TypeError (:issue:`23079`) - Bug in :func:`date_range` with frequency of ``Day`` or higher where dates sufficiently far in the future could wrap around to the past instead of raising ``OutOfBoundsDatetime`` (:issue:`14187`) - Bug in :class:`PeriodIndex` with attribute ``freq.n`` greater than 1 where adding a :class:`DateOffset` object would return incorrect results (:issue:`23215`) +- Bug in :class:`Series` that interpreted string indices as lists of characters when setting datetimelike values (:issue:`23451`) Timedelta ^^^^^^^^^ diff --git a/pandas/core/series.py b/pandas/core/series.py index cb8371ba086ba..6971b0b0c78e0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -947,7 +947,9 @@ def _set_with(self, key, value): except Exception: pass - if not isinstance(key, (list, Series, np.ndarray, Series)): + if is_scalar(key): + key = [key] + elif not isinstance(key, (list, Series, np.ndarray)): try: key = list(key) except Exception: diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 2f6efc112819c..f3ae2b1e6ad15 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -548,3 +548,10 @@ def test_minmax_nat_series(self, nat): def test_minmax_nat_dataframe(self, nat): assert nat.min()[0] is pd.NaT assert nat.max()[0] is pd.NaT + + def test_setitem_with_string_index(self): + # GH 23451 + x = pd.Series([1, 2, 3], index=['Date', 'b', 'other']) + x['Date'] = date.today() + assert x.Date == date.today() + assert x['Date'] == date.today()