From 9b9ab112f8ff9eb5f2554cb19e6b589948959e2f Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 15 Jan 2024 16:57:12 +0100 Subject: [PATCH 1/2] Fix setitem parametrizations and raise correct Valueerror when pd.NA present --- pandas/core/internals/blocks.py | 7 ++++++- pandas/tests/extension/base/setitem.py | 12 ++++++------ pandas/tests/extension/json/test_json.py | 17 +++++++++++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 1237c5b86d298..c27708b4cbbfe 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -122,7 +122,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: @@ -2051,6 +2054,8 @@ def setitem(self, indexer, value, using_cow: bool = False): # TODO(GH#45419): string[pyarrow] tests break if we transpose # unconditionally values = values.T + + check_array_indexer(self, 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 ba756b471eb8b..8a69ceb731d12 100644 --- a/pandas/tests/extension/base/setitem.py +++ b/pandas/tests/extension/base/setitem.py @@ -205,12 +205,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"], ) @@ -224,7 +221,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 50f2fe03cfa13..44925170affe5 100644 --- a/pandas/tests/extension/json/test_json.py +++ b/pandas/tests/extension/json/test_json.py @@ -346,15 +346,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"], From 0b9f7f0361490daf38a064975817306ebfa150fe Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 22 Jan 2024 11:16:02 +0100 Subject: [PATCH 2/2] fix mypy incompatible type --- pandas/core/internals/blocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index c27708b4cbbfe..6b480f0d79480 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2055,7 +2055,7 @@ def setitem(self, indexer, value, using_cow: bool = False): # unconditionally values = values.T - check_array_indexer(self, indexer) + check_array_indexer(values, indexer) check_setitem_lengths(indexer, value, values) try: