diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 80c8a1e8ef5c7..bd19aed631659 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -115,7 +115,10 @@ ensure_wrapped_if_datetimelike, extract_array, ) -from pandas.core.indexers import check_setitem_lengths +from pandas.core.indexers import ( + check_array_indexer, + check_setitem_lengths, +) from pandas.core.indexes.base import get_values_for_csv if TYPE_CHECKING: @@ -1661,6 +1664,8 @@ def setitem(self, indexer, value): # TODO(GH#45419): string[pyarrow] tests break if we transpose # unconditionally values = values.T + + check_array_indexer(values, indexer) check_setitem_lengths(indexer, value, values) try: diff --git a/pandas/tests/extension/base/setitem.py b/pandas/tests/extension/base/setitem.py index 3fb2fc09eaa79..4410b3d0441af 100644 --- a/pandas/tests/extension/base/setitem.py +++ b/pandas/tests/extension/base/setitem.py @@ -206,12 +206,9 @@ def test_setitem_integer_array(self, data, idx, box_in_series): "idx, box_in_series", [ ([0, 1, 2, pd.NA], False), - pytest.param( - [0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948") - ), + ([0, 1, 2, pd.NA], True), (pd.array([0, 1, 2, pd.NA], dtype="Int64"), False), - # TODO: change False to True? - (pd.array([0, 1, 2, pd.NA], dtype="Int64"), False), # noqa: PT014 + (pd.array([0, 1, 2, pd.NA], dtype="Int64"), True), ], ids=["list-False", "list-True", "integer-array-False", "integer-array-True"], ) @@ -225,7 +222,10 @@ def test_setitem_integer_with_missing_raises(self, data, idx, box_in_series): msg = "Cannot index with an integer indexer containing NA values" with pytest.raises(ValueError, match=msg): - arr[idx] = arr[0] + if type(arr) is pd.Series: + arr.iloc[idx] = arr.iloc[0] + else: + arr[idx] = arr[0] @pytest.mark.parametrize("as_callable", [True, False]) @pytest.mark.parametrize("setter", ["loc", None]) diff --git a/pandas/tests/extension/json/test_json.py b/pandas/tests/extension/json/test_json.py index 6ecbf2063f203..9dd9fe5977f04 100644 --- a/pandas/tests/extension/json/test_json.py +++ b/pandas/tests/extension/json/test_json.py @@ -342,15 +342,24 @@ def test_setitem_integer_array(self, data, idx, box_in_series, request): request.applymarker(mark) super().test_setitem_integer_array(data, idx, box_in_series) - @pytest.mark.xfail(reason="list indices must be integers or slices, not NAType") @pytest.mark.parametrize( "idx, box_in_series", [ - ([0, 1, 2, pd.NA], False), pytest.param( - [0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948") + [0, 1, 2, pd.NA], + False, + marks=pytest.mark.xfail( + reason="Cannot index with an integer indexer containing NA values" + ), + ), + ([0, 1, 2, pd.NA], True), + pytest.param( + pd.array([0, 1, 2, pd.NA], dtype="Int64"), + False, + marks=pytest.mark.xfail( + reason="Cannot index with an integer indexer containing NA values" + ), ), - (pd.array([0, 1, 2, pd.NA], dtype="Int64"), False), (pd.array([0, 1, 2, pd.NA], dtype="Int64"), True), ], ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],