Skip to content

BUG: Inserting array of same size with Series.loc raises ValueError #38266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 5, 2020
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ Indexing
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.__getitem__` raising ``KeyError`` when columns were :class:`MultiIndex` with only one level (:issue:`29749`)
- Bug in :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__` raising blank ``KeyError`` without missing keys for :class:`IntervalIndex` (:issue:`27365`)
- Bug in setting a new label on a :class:`DataFrame` or :class:`Series` with a :class:`CategoricalIndex` incorrectly raising ``TypeError`` when the new label is not among the index's categories (:issue:`38098`)
- Bug in :meth:`Series.loc` and :meth:`Series.iloc` raising ``ValueError`` when inserting an array in a ``object`` Series of equal length (:issue:`37748`)
Copy link
Member

Choose a reason for hiding this comment

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

array seems vague. Do you mean NumPy array, list, ExtensionArray (or selection of those)

Copy link
Member

Choose a reason for hiding this comment

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

all of those i guess :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, np.array, list, tuple do cause the raise. From your example, ExtensionArray don't but also behave unexpectedly.


Missing
^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def is_scalar_indexer(indexer, ndim: int) -> bool:
-------
bool
"""
if ndim == 1 and is_integer(indexer):
# GH37748: allow indexer to be an integer for Series
return True
if isinstance(indexer, tuple):
if len(indexer) == ndim:
return all(
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,3 +1042,14 @@ def test_iloc_setitem_pure_position_based(self):
ser1.iloc[1:3] = ser2.iloc[1:3]
expected = Series([1, 5, 6])
tm.assert_series_equal(ser1, expected)

@pytest.mark.parametrize("size", [0, 4, 5, 6])
def test_iloc_setitem_with_array(self, size):
# GH37748
ser = Series(0, index=range(5), dtype="object")

expected = np.zeros(size)
Copy link
Member

Choose a reason for hiding this comment

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

would you get the same bug with a length size python list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

ser.iloc[0] = expected
result = ser[0]

tm.assert_numpy_array_equal(result, expected)
6 changes: 6 additions & 0 deletions pandas/tests/indexing/test_indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def test_is_scalar_indexer():

assert not is_scalar_indexer(slice(None), 1)

indexer = 0
assert is_scalar_indexer(indexer, 1)

indexer = (0,)
assert is_scalar_indexer(indexer, 1)


class TestValidateIndices:
def test_validate_indices_ok(self):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2072,3 +2072,14 @@ def test_loc_setitem_dt64tz_values(self):
s2["a"] = expected
result = s2["a"]
assert result == expected

@pytest.mark.parametrize("size", [0, 4, 5, 6])
def test_loc_setitem_with_array(self, size):
# GH37748
ser = Series(0, index=list("abcde"), dtype="object")

expected = np.zeros(size)
ser.loc["a"] = expected
result = ser[0]

tm.assert_numpy_array_equal(result, expected)