Skip to content

Commit

Permalink
Backport PR pandas-dev#54641: BUG: getitem indexing wrong axis
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored and meeseeksmachine committed Aug 21, 2023
1 parent dc04d00 commit b10fd84
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 b10fd84

Please sign in to comment.