From 8f383252b26d21abfbbe1b2e46bcd2b1b55b8295 Mon Sep 17 00:00:00 2001 From: Konstantin Malanchev Date: Tue, 7 May 2024 14:00:33 +0000 Subject: [PATCH 1/5] Failing test_setitem_integer_array_with_repeats --- pandas/tests/extension/base/setitem.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pandas/tests/extension/base/setitem.py b/pandas/tests/extension/base/setitem.py index 3fb2fc09eaa79..a455b21b9932a 100644 --- a/pandas/tests/extension/base/setitem.py +++ b/pandas/tests/extension/base/setitem.py @@ -202,6 +202,22 @@ def test_setitem_integer_array(self, data, idx, box_in_series): arr[idx] = arr[0] tm.assert_equal(arr, expected) + @pytest.mark.parametrize( + "idx", + [[0, 0, 1], pd.array([0, 0, 1], dtype="Int64"), np.array([0, 0, 1])], + ids=["list", "integer-array", "numpy-array"], + ) + def test_setitem_integer_array_with_repeats(self, data, idx, box_in_series): + arr = data[:5].copy() + expected = data.take([2, 3, 2, 3, 4]) + + if box_in_series: + arr = pd.Series(arr) + expected = pd.Series(expected) + + arr[idx] = [arr[2], arr[2], arr[3]] + tm.assert_equal(arr, expected) + @pytest.mark.parametrize( "idx, box_in_series", [ From b038a40e3ac3f3332fb946ebb99f8e80ae5d63e8 Mon Sep 17 00:00:00 2001 From: Konstantin Malanchev Date: Tue, 7 May 2024 14:15:35 +0000 Subject: [PATCH 2/5] Arrow ext: fix assignment by repeated key --- pandas/core/arrays/arrow/array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py index 0240433cdb683..96e77e4b920d6 100644 --- a/pandas/core/arrays/arrow/array.py +++ b/pandas/core/arrays/arrow/array.py @@ -1880,7 +1880,8 @@ def __setitem__(self, key, value) -> None: raise ValueError("Length of indexer and values mismatch") if len(indices) == 0: return - argsort = np.argsort(indices) + # GH#58530 wrong item assignment by repeated key + _, argsort = np.unique(indices, return_index=True) indices = indices[argsort] value = value.take(argsort) mask = np.zeros(len(self), dtype=np.bool_) From 53bdd7d6dd415673fa5a5f65e00fbbf7fcae39f3 Mon Sep 17 00:00:00 2001 From: Konstantin Malanchev Date: Tue, 7 May 2024 14:53:09 +0000 Subject: [PATCH 3/5] Update whatsnew/v3.0.0.rst --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 278971ef88a0f..1882e73d8c3d6 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -466,6 +466,7 @@ Sparse ExtensionArray ^^^^^^^^^^^^^^ - Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`) +- Bug in :meth:`core.arrays.ExtensionArray.__setitem__` which caused wrong behaviour when using an integer array with repeated values as a key (:issue:`58530`) Styler ^^^^^^ From aa0722b5275cbc1b8a95efff214fdf505e470528 Mon Sep 17 00:00:00 2001 From: Konstantin Malanchev Date: Tue, 7 May 2024 13:38:20 -0400 Subject: [PATCH 4/5] Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 1882e73d8c3d6..db27f680e48db 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -466,7 +466,7 @@ Sparse ExtensionArray ^^^^^^^^^^^^^^ - Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`) -- Bug in :meth:`core.arrays.ExtensionArray.__setitem__` which caused wrong behaviour when using an integer array with repeated values as a key (:issue:`58530`) +- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`) Styler ^^^^^^ From e9ae0aa088597a92d8dc3c00b7363a5fbe0835b6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 May 2024 17:43:27 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index db27f680e48db..5cb5e91f74e3f 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -465,8 +465,8 @@ Sparse ExtensionArray ^^^^^^^^^^^^^^ -- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`) - Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`) +- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`) Styler ^^^^^^