Skip to content

Backport PR #45785 on branch 1.4.x (Regression: Loc raising when indexing mi with one level) #45895

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
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.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Regression in :meth:`Series.mask` with ``inplace=True`` and ``PeriodDtype`` and an incompatible ``other`` coercing to a common dtype instead of raising (:issue:`45546`)
- Regression in :func:`.assert_frame_equal` not respecting ``check_flags=False`` (:issue:`45554`)
- Regression in :meth:`DataFrame.loc.__getitem__` raising ``ValueError`` when indexing on a :class:`MultiIndex` with one level (:issue:`45779`)
- Regression in :meth:`Series.fillna` with ``downcast=False`` incorrectly downcasting ``object`` dtype (:issue:`45603`)
- Regression in :func:`api.types.is_bool_dtype` raising an ``AttributeError`` when evaluating a categorical :class:`Series` (:issue:`45615`)
- Regression in :meth:`DataFrame.iat` setting values leading to not propagating correctly in subsequent lookups (:issue:`45684`)
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2986,6 +2986,10 @@ def _get_loc_level(self, key, level: int | list[int] = 0):

# different name to distinguish from maybe_droplevels
def maybe_mi_droplevels(indexer, levels):
"""
If level does not exist or all levels were dropped, the exception
has to be handled outside.
"""
new_index = self[indexer]

for i in sorted(levels, reverse=True):
Expand Down Expand Up @@ -3119,7 +3123,12 @@ def maybe_mi_droplevels(indexer, levels):
# e.g. test_partial_string_timestamp_multiindex
return indexer, self[indexer]

return indexer, maybe_mi_droplevels(indexer, [level])
try:
result_index = maybe_mi_droplevels(indexer, [level])
except ValueError:
result_index = self[indexer]

return indexer, result_index

def _get_level_indexer(
self, key, level: int = 0, indexer: Int64Index | None = None
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,18 @@ def test_loc_iloc_setitem_non_categorical_rhs(
with pytest.raises(TypeError, match=msg1):
indexer(df)[key] = ["c", "c"]

def test_loc_on_multiindex_one_level(self):
# GH#45779
df = DataFrame(
data=[[0], [1]],
index=MultiIndex.from_tuples([("a",), ("b",)], names=["first"]),
)
expected = DataFrame(
data=[[0]], index=MultiIndex.from_tuples([("a",)], names=["first"])
)
result = df.loc["a"]
tm.assert_frame_equal(result, expected)


class TestDepreactedIndexers:
@pytest.mark.parametrize(
Expand Down