Skip to content

Commit

Permalink
BUG: DataFrame slice selection treated as hashable in Python 3.12 #57500
Browse files Browse the repository at this point in the history
 (#58043)

* Reorder slice and hashable in __getitem__

* Add unit test

* Fix test and formatting

* Update whatsnew

* Restore original flow ordering

* Move whatsnew entry to 3.0.0

* Move whatsnew entry to Indexing

* Update doc/source/whatsnew/v3.0.0.rst

---------

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
tehunter and mroeschke committed Apr 16, 2024
1 parent f8bd988 commit 53bd1a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Interval

Indexing
^^^^^^^^
-
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
-

Missing
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3855,8 +3855,10 @@ def __getitem__(self, key):
key = lib.item_from_zerodim(key)
key = com.apply_if_callable(key, self)

if is_hashable(key) and not is_iterator(key):
if is_hashable(key) and not is_iterator(key) and not isinstance(key, slice):
# is_iterator to exclude generator e.g. test_getitem_listlike
# As of Python 3.12, slice is hashable which breaks MultiIndex (GH#57500)

# shortcut if the key is in columns
is_mi = isinstance(self.columns, MultiIndex)
# GH#45316 Return view if key is not duplicated
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,16 @@ def test_loc_setitem_boolean_mask_allfalse(self):
result.loc[result.b.isna(), "a"] = result.a.copy()
tm.assert_frame_equal(result, df)

def test_getitem_slice_empty(self):
df = DataFrame([[1]], columns=MultiIndex.from_product([["A"], ["a"]]))
result = df[:]

expected = DataFrame([[1]], columns=MultiIndex.from_product([["A"], ["a"]]))

tm.assert_frame_equal(result, expected)
# Ensure df[:] returns a view of df, not the same object
assert result is not df

def test_getitem_fancy_slice_integers_step(self):
df = DataFrame(np.random.default_rng(2).standard_normal((10, 5)))

Expand Down

0 comments on commit 53bd1a8

Please sign in to comment.