Skip to content

Commit

Permalink
Backport PR #48178 on branch 1.4.x (REGR: loc not working with NamedT…
Browse files Browse the repository at this point in the history
…uple) (#48217)

Backport PR #48178: REGR: loc not working with NamedTuple

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Aug 23, 2022
1 parent c92c953 commit 4c60b14
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Expand Up @@ -20,6 +20,7 @@ Fixed regressions
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on Index objects (:issue:`46769`)
- 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
Expand Up @@ -1175,6 +1175,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
@@ -1,3 +1,4 @@
from collections import namedtuple
from datetime import (
datetime,
timedelta,
Expand Down Expand Up @@ -1339,6 +1340,23 @@ def test_loc_setitem_rhs_frame(self, idxr, val):
expected = DataFrame({"a": [np.nan, val]})
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

0 comments on commit 4c60b14

Please sign in to comment.