Skip to content

REGR: loc not working with NamedTuple #48178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
- Fixed regression in :func:`cut` using a ``datetime64`` IntervalIndex as bins (:issue:`46218`)
- Fixed regression in :meth:`DataFrame.select_dtypes` where ``include="number"`` included :class:`BooleanDtype` (:issue:`46870`)
- Fixed regression in :meth:`DataFrame.loc` raising error when indexing with a ``NamedTuple`` (:issue:`48124`)
- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,9 @@ def _getitem_axis(self, key, axis: int):

labels = self.obj._get_axis(axis)

if isinstance(key, tuple) and isinstance(labels, MultiIndex):
key = tuple(key)

if isinstance(key, slice):
self._validate_key(key, axis)
return self._get_slice_axis(key, axis=axis)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import namedtuple
from datetime import (
datetime,
timedelta,
Expand Down Expand Up @@ -1371,6 +1372,23 @@ def test_iloc_setitem_ea_null_slice_length_one_list(self, func):
expected = DataFrame({"a": [5, 5, 5]}, dtype="Int64")
tm.assert_frame_equal(df, expected)

def test_loc_named_tuple_for_midx(self):
# GH#48124
df = DataFrame(
index=MultiIndex.from_product(
[["A", "B"], ["a", "b", "c"]], names=["first", "second"]
)
)
indexer_tuple = namedtuple("Indexer", df.index.names)
idxr = indexer_tuple(first="A", second=["a", "b"])
result = df.loc[idxr, :]
expected = DataFrame(
index=MultiIndex.from_tuples(
[("A", "a"), ("A", "b")], names=["first", "second"]
)
)
tm.assert_frame_equal(result, expected)


class TestDataFrameIndexingUInt64:
def test_setitem(self, uint64_frame):
Expand Down