Skip to content

Commit 0a4ba64

Browse files
authored
BUG: unwrap setitem indexer for ArrowExtensionArray (#50085)
* unwrap setitem indexer for 1D-only extension arrays * fix test * gh refs * fix whatsnew * update test * move fix to ArrowExtensionArray.__setitem__ * remove typing * remove typing override
1 parent 02d22f3 commit 0a4ba64

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ Indexing
874874
- Bug in :meth:`DataFrame.loc` coercing dtypes when setting values with a list indexer (:issue:`49159`)
875875
- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`)
876876
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
877+
- Bug in :meth:`DataFrame.loc` raising ``IndexError`` when setting values for a pyarrow-backed column with a non-scalar indexer (:issue:`50085`)
877878
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
878879
- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`)
879880
- Bug in :meth:`DataFrame.iloc` raising ``IndexError`` when indexer is a :class:`Series` with numeric extension array dtype (:issue:`49521`)

pandas/core/arrays/arrow/array.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
983983
return self.dtype.na_value
984984
return result.as_py()
985985

986-
def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None:
986+
def __setitem__(self, key, value) -> None:
987987
"""Set one or more values inplace.
988988
989989
Parameters
@@ -1004,6 +1004,10 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None:
10041004
-------
10051005
None
10061006
"""
1007+
# GH50085: unwrap 1D indexers
1008+
if isinstance(key, tuple) and len(key) == 1:
1009+
key = key[0]
1010+
10071011
key = check_array_indexer(self, key)
10081012
value = self._maybe_convert_setitem_value(value)
10091013

@@ -1029,7 +1033,6 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None:
10291033
return
10301034

10311035
indices = self._indexing_key_to_indices(key)
1032-
10331036
argsort = np.argsort(indices)
10341037
indices = indices[argsort]
10351038

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def __getitem__(
348348
"""
349349
raise AbstractMethodError(self)
350350

351-
def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None:
351+
def __setitem__(self, key, value) -> None:
352352
"""
353353
Set one or more values inplace.
354354

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def _get_getitem_freq(self, key) -> BaseOffset | None:
407407
# error: Argument 1 of "__setitem__" is incompatible with supertype
408408
# "ExtensionArray"; supertype defines the argument type as "Union[int,
409409
# ndarray]"
410-
def __setitem__( # type: ignore[override]
410+
def __setitem__(
411411
self,
412412
key: int | Sequence[int] | Sequence[bool] | slice,
413413
value: NaTType | Any | Sequence[Any],

pandas/tests/extension/base/setitem.py

+8
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,11 @@ def test_setitem_invalid(self, data, invalid_scalar):
418418

419419
with pytest.raises((ValueError, TypeError), match=msg):
420420
data[:] = invalid_scalar
421+
422+
def test_setitem_2d_values(self, data):
423+
# GH50085
424+
original = data.copy()
425+
df = pd.DataFrame({"a": data, "b": data})
426+
df.loc[[0, 1], :] = df.loc[[1, 0], :].values
427+
assert (df.loc[0, :] == original[1]).all()
428+
assert (df.loc[1, :] == original[0]).all()

0 commit comments

Comments
 (0)