Skip to content

Commit 6e579ed

Browse files
authored
BUG: Series.setitem raising ValueError when setting Series with scalar indexer (#39358)
1 parent 201d263 commit 6e579ed

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ Indexing
291291
- Bug in :meth:`DataFrame.loc.__setitem__` raising ValueError when expanding unique column for :class:`DataFrame` with duplicate columns (:issue:`38521`)
292292
- Bug in :meth:`DataFrame.iloc.__setitem__` and :meth:`DataFrame.loc.__setitem__` with mixed dtypes when setting with a dictionary value (:issue:`38335`)
293293
- Bug in :meth:`DataFrame.__setitem__` not raising ``ValueError`` when right hand side is a :class:`DataFrame` with wrong number of columns (:issue:`38604`)
294+
- Bug in :meth:`Series.__setitem__` raising ``ValueError`` when setting a :class:`Series` with a scalar indexer (:issue:`38303`)
294295
- Bug in :meth:`DataFrame.loc` dropping levels of :class:`MultiIndex` when :class:`DataFrame` used as input has only one row (:issue:`10521`)
295296
- Bug in :meth:`DataFrame.__getitem__` and :meth:`Series.__getitem__` always raising ``KeyError`` when slicing with existing strings an :class:`Index` with milliseconds (:issue:`33589`)
296297
- Bug in setting ``timedelta64`` values into numeric :class:`Series` failing to cast to object dtype (:issue:`39086`)

pandas/core/indexing.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,17 @@ def ravel(i):
20332033
return ser._values.copy()
20342034
return ser.reindex(ax)._values
20352035

2036-
elif is_scalar(indexer):
2036+
elif is_integer(indexer) and self.ndim == 1:
2037+
if is_object_dtype(self.obj):
2038+
return ser
2039+
ax = self.obj._get_axis(0)
2040+
2041+
if ser.index.equals(ax):
2042+
return ser._values.copy()
2043+
2044+
return ser.reindex(ax)._values[indexer]
2045+
2046+
elif is_integer(indexer):
20372047
ax = self.obj._get_axis(1)
20382048

20392049
if ser.index.equals(ax):

pandas/tests/series/indexing/test_setitem.py

+18
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ def test_setitem_negative_out_of_bounds(self):
9393
with pytest.raises(IndexError, match=msg):
9494
ser[-11] = "foo"
9595

96+
@pytest.mark.parametrize("indexer", [tm.loc, tm.at])
97+
@pytest.mark.parametrize("ser_index", [0, 1])
98+
def test_setitem_series_object_dtype(self, indexer, ser_index):
99+
# GH#38303
100+
ser = Series([0, 0], dtype="object")
101+
idxr = indexer(ser)
102+
idxr[0] = Series([42], index=[ser_index])
103+
expected = Series([Series([42], index=[ser_index]), 0], dtype="object")
104+
tm.assert_series_equal(ser, expected)
105+
106+
@pytest.mark.parametrize("index, exp_value", [(0, 42.0), (1, np.nan)])
107+
def test_setitem_series(self, index, exp_value):
108+
# GH#38303
109+
ser = Series([0, 0])
110+
ser.loc[0] = Series([42], index=[index])
111+
expected = Series([exp_value, 0])
112+
tm.assert_series_equal(ser, expected)
113+
96114

97115
class TestSetitemSlices:
98116
def test_setitem_slice_float_raises(self, datetime_series):

0 commit comments

Comments
 (0)