Skip to content

Commit 5399c6d

Browse files
authored
BUG: Inserting array of same size with Series.loc raises ValueError (#38266)
1 parent 7073ee1 commit 5399c6d

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

doc/source/whatsnew/v1.2.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ Indexing
678678
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.__getitem__` raising ``KeyError`` when columns were :class:`MultiIndex` with only one level (:issue:`29749`)
679679
- Bug in :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__` raising blank ``KeyError`` without missing keys for :class:`IntervalIndex` (:issue:`27365`)
680680
- 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`)
681+
- Bug in :meth:`Series.loc` and :meth:`Series.iloc` raising ``ValueError`` when inserting a listlike ``np.array``, ``list`` or ``tuple`` in an ``object`` Series of equal length (:issue:`37748`, :issue:`37486`)
682+
- Bug in :meth:`Series.loc` and :meth:`Series.iloc` setting all the values of an ``object`` Series with those of a listlike ``ExtensionArray`` instead of inserting it (:issue:`38271`)
681683

682684
Missing
683685
^^^^^^^

pandas/core/indexers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def is_scalar_indexer(indexer, ndim: int) -> bool:
7979
-------
8080
bool
8181
"""
82+
if ndim == 1 and is_integer(indexer):
83+
# GH37748: allow indexer to be an integer for Series
84+
return True
8285
if isinstance(indexer, tuple):
8386
if len(indexer) == ndim:
8487
return all(

pandas/tests/indexing/test_indexers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def test_is_scalar_indexer():
2828

2929
assert not is_scalar_indexer(slice(None), 1)
3030

31+
indexer = 0
32+
assert is_scalar_indexer(indexer, 1)
33+
34+
indexer = (0,)
35+
assert is_scalar_indexer(indexer, 1)
36+
3137

3238
class TestValidateIndices:
3339
def test_validate_indices_ok(self):

pandas/tests/indexing/test_loc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,3 +2072,21 @@ def test_loc_setitem_dt64tz_values(self):
20722072
s2["a"] = expected
20732073
result = s2["a"]
20742074
assert result == expected
2075+
2076+
@pytest.mark.parametrize("array_fn", [np.array, pd.array, list, tuple])
2077+
@pytest.mark.parametrize("size", [0, 4, 5, 6])
2078+
def test_loc_iloc_setitem_with_listlike(self, size, array_fn):
2079+
# GH37748
2080+
# testing insertion, in a Series of size N (here 5), of a listlike object
2081+
# of size 0, N-1, N, N+1
2082+
2083+
arr = array_fn([0] * size)
2084+
expected = Series([arr, 0, 0, 0, 0], index=list("abcde"), dtype=object)
2085+
2086+
ser = Series(0, index=list("abcde"), dtype=object)
2087+
ser.loc["a"] = arr
2088+
tm.assert_series_equal(ser, expected)
2089+
2090+
ser = Series(0, index=list("abcde"), dtype=object)
2091+
ser.iloc[0] = arr
2092+
tm.assert_series_equal(ser, expected)

0 commit comments

Comments
 (0)