Skip to content
Prev Previous commit
Next Next commit
Handle non object dtype series
  • Loading branch information
phofl committed Jan 23, 2021
commit 2cb7a7b9e97a88d9e0cb93408a3887dca82b3a61
9 changes: 8 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,14 @@ def ravel(i):
return ser.reindex(ax)._values

elif is_scalar(indexer) and isinstance(self.obj, ABCSeries):
return ser
if is_object_dtype(self.obj):
return ser
ax = self.obj._get_axis(0)

if ser.index.equals(ax):
return ser._values.copy()

return ser.reindex(ax)._values[[indexer]]

elif is_scalar(indexer):
ax = self.obj._get_axis(1)
Expand Down
17 changes: 13 additions & 4 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,20 @@ def test_setitem_negative_out_of_bounds(self):
with pytest.raises(IndexError, match=msg):
ser[-11] = "foo"

def test_setitem_series(self):
@pytest.mark.parametrize("ser_index", [0, 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize over tm.loc and tm.at (i assume that .iloc / .iat take a different path)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, did not know that. Yes they do not align

def test_setitem_series_object_dtype(self, ser_index):
# GH#38303
ser = Series(0, index=[0, 1, 2])
ser[0] = Series([42])
expected = Series([42, 0, 0])
ser = Series([0, 0], dtype="object")
ser.loc[0] = Series([42], index=[ser_index])
expected = Series([Series([42], index=[ser_index]), 0], dtype="object")
tm.assert_series_equal(ser, expected)

@pytest.mark.parametrize("index, exp_value", [(0, 42.0), (1, np.nan)])
def test_setitem_series(self, index, exp_value):
# GH#38303
ser = Series([0, 0])
ser.loc[0] = Series([42], index=[index])
expected = Series([exp_value, 0])
tm.assert_series_equal(ser, expected)


Expand Down