Skip to content

Commit 6dbfd42

Browse files
JustinZhengBCPingviinituutti
authored andcommitted
BUG GH23451 Allow setting date in string index for Series (pandas-dev#23495)
1 parent 9b8294b commit 6dbfd42

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ Datetimelike
11181118
- Bug in :func:`DataFrame.combine` with datetimelike values raising a TypeError (:issue:`23079`)
11191119
- 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`)
11201120
- Bug in :class:`PeriodIndex` with attribute ``freq.n`` greater than 1 where adding a :class:`DateOffset` object would return incorrect results (:issue:`23215`)
1121+
- Bug in :class:`Series` that interpreted string indices as lists of characters when setting datetimelike values (:issue:`23451`)
11211122

11221123
Timedelta
11231124
^^^^^^^^^

pandas/core/series.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,9 @@ def _set_with(self, key, value):
947947
except Exception:
948948
pass
949949

950-
if not isinstance(key, (list, Series, np.ndarray, Series)):
950+
if is_scalar(key):
951+
key = [key]
952+
elif not isinstance(key, (list, Series, np.ndarray)):
951953
try:
952954
key = list(key)
953955
except Exception:

pandas/tests/series/test_datetime_values.py

+7
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,10 @@ def test_minmax_nat_series(self, nat):
548548
def test_minmax_nat_dataframe(self, nat):
549549
assert nat.min()[0] is pd.NaT
550550
assert nat.max()[0] is pd.NaT
551+
552+
def test_setitem_with_string_index(self):
553+
# GH 23451
554+
x = pd.Series([1, 2, 3], index=['Date', 'b', 'other'])
555+
x['Date'] = date.today()
556+
assert x.Date == date.today()
557+
assert x['Date'] == date.today()

0 commit comments

Comments
 (0)