From 744309c50bea6c01ac16c3fbdf5fe6acd17f3044 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 20 Nov 2021 20:11:55 -0800 Subject: [PATCH 1/2] BUG: Indexing on nullable column --- doc/source/whatsnew/v1.4.0.rst | 2 ++ pandas/core/internals/blocks.py | 4 +++- pandas/tests/indexing/test_iloc.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index b962add29db33..c7ce958b49c26 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -616,6 +616,8 @@ Indexing - Bug in :meth:`Series.__setitem__` with a boolean mask indexer setting a listlike value of length 1 incorrectly broadcasting that value (:issue:`44265`) - Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`DataFrame.iloc.__setitem__` with mixed dtypes sometimes failing to operate in-place (:issue:`44345`) - Bug in :meth:`DataFrame.loc.__getitem__` incorrectly raising ``KeyError`` when selecting a single column with a boolean key (:issue:`44322`). +- Bug in indexing on columns with ``loc`` or ``iloc`` using a slice with a negative step with ``ExtensionDtype`` columns incorrectly raising (:issue:`??`) +- Missing ^^^^^^^ diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a6fa2a9e3b2c1..963b3c49eb054 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1566,7 +1566,9 @@ def _slice(self, slicer) -> ExtensionArray: ) # GH#32959 only full-slicers along fake-dim0 are valid # TODO(EA2D): won't be necessary with 2D EAs - new_locs = self._mgr_locs[first] + # range(1) instead of self._mgr_locs to avoid exception on [::-1] + # see test_iloc_getitem_slice_negative_step_ea_block + new_locs = range(1)[first] if len(new_locs): # effectively slice(None) slicer = slicer[1] diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 7d2f68b00d95f..3606adf9af573 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -1148,6 +1148,17 @@ def test_loc_setitem_boolean_list(self, rhs_func, indexing_func): expected = DataFrame({"a": [5, 1, 10]}) tm.assert_frame_equal(df, expected) + def test_iloc_getitem_slice_negative_step_ea_block(self): + df = DataFrame({"A": [1, 2, 3]}, dtype="Int64") + + res = df.iloc[:, ::-1] + tm.assert_frame_equal(res, df) + + df["B"] = "foo" + res = df.iloc[:, ::-1] + expected = DataFrame({"B": df["B"], "A": df["A"]}) + tm.assert_frame_equal(res, expected) + class TestILocErrors: # NB: this test should work for _any_ Series we can pass as From 84c11bf2d8c482dde74e0bc5e0762ce153e20bec Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 20 Nov 2021 20:13:21 -0800 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/tests/indexing/test_iloc.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index c7ce958b49c26..5bba04d3e9dcb 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -616,7 +616,7 @@ Indexing - Bug in :meth:`Series.__setitem__` with a boolean mask indexer setting a listlike value of length 1 incorrectly broadcasting that value (:issue:`44265`) - Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`DataFrame.iloc.__setitem__` with mixed dtypes sometimes failing to operate in-place (:issue:`44345`) - Bug in :meth:`DataFrame.loc.__getitem__` incorrectly raising ``KeyError`` when selecting a single column with a boolean key (:issue:`44322`). -- Bug in indexing on columns with ``loc`` or ``iloc`` using a slice with a negative step with ``ExtensionDtype`` columns incorrectly raising (:issue:`??`) +- Bug in indexing on columns with ``loc`` or ``iloc`` using a slice with a negative step with ``ExtensionDtype`` columns incorrectly raising (:issue:`44551`) - Missing diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 3606adf9af573..2b94070fd76b9 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -1149,6 +1149,7 @@ def test_loc_setitem_boolean_list(self, rhs_func, indexing_func): tm.assert_frame_equal(df, expected) def test_iloc_getitem_slice_negative_step_ea_block(self): + # GH#44551 df = DataFrame({"A": [1, 2, 3]}, dtype="Int64") res = df.iloc[:, ::-1]