Skip to content

Commit

Permalink
Backport PR #54641 on branch 2.1.x (BUG: getitem indexing wrong axis) (
Browse files Browse the repository at this point in the history
…#54669)

Backport PR #54641: BUG: getitem indexing wrong axis

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Aug 21, 2023
1 parent dc04d00 commit 23cb250
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,9 @@ def _getitem_tuple_same_dim(self, tup: tuple):
"""
retval = self.obj
# Selecting columns before rows is signficiantly faster
start_val = (self.ndim - len(tup)) + 1
for i, key in enumerate(reversed(tup)):
i = self.ndim - i - 1
i = self.ndim - i - start_val
if com.is_null_slice(key):
continue

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@ def test_getitem_datetime_slice(self):
):
df["2011-01-01":"2011-11-01"]

def test_getitem_slice_same_dim_only_one_axis(self):
# GH#54622
df = DataFrame(np.random.default_rng(2).standard_normal((10, 8)))
result = df.iloc[(slice(None, None, 2),)]
assert result.shape == (5, 8)
expected = df.iloc[slice(None, None, 2), slice(None)]
tm.assert_frame_equal(result, expected)


class TestGetitemDeprecatedIndexers:
@pytest.mark.parametrize("key", [{"a", "b"}, {"a": "a"}])
Expand Down

0 comments on commit 23cb250

Please sign in to comment.