Skip to content

BUG: MultiIndex.get_loc with string key on DatetimeIndex level #42465

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 4 commits into from
Jul 16, 2021
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.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ Missing

MultiIndex
^^^^^^^^^^
- Bug in :meth:`MultiIndex.get_loc` where the first level is a :class:`DatetimeIndex` and a string key is passed (:issue:`42465`)
- Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`)
-

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3160,10 +3160,12 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
if level > 0 or self._lexsort_depth == 0:
# Desired level is not sorted
if isinstance(idx, slice):
# test_get_loc_partial_timestamp_multiindex
locs = (level_codes >= idx.start) & (level_codes < idx.stop)
return locs

locs = np.array(level_codes == idx, dtype=bool, copy=False)

if not locs.any():
# The label is present in self.levels[level] but unused:
raise KeyError(key)
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/indexes/multi/test_partial_indexing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest

from pandas import (
Expand Down Expand Up @@ -45,6 +46,42 @@ def test_partial_string_matching_single_index(df):
tm.assert_frame_equal(result, expected)


def test_get_loc_partial_timestamp_multiindex(df):
mi = df.index
key = ("2016-01-01", "a")
loc = mi.get_loc(key)

expected = np.zeros(len(mi), dtype=bool)
expected[[0, 3]] = True
tm.assert_numpy_array_equal(loc, expected)

key2 = ("2016-01-02", "a")
loc2 = mi.get_loc(key2)
expected2 = np.zeros(len(mi), dtype=bool)
expected2[[6, 9]] = True
tm.assert_numpy_array_equal(loc2, expected2)

key3 = ("2016-01", "a")
loc3 = mi.get_loc(key3)
expected3 = np.zeros(len(mi), dtype=bool)
expected3[mi.get_level_values(1).get_loc("a")] = True
tm.assert_numpy_array_equal(loc3, expected3)

key4 = ("2016", "a")
loc4 = mi.get_loc(key4)
expected4 = expected3
tm.assert_numpy_array_equal(loc4, expected4)

# non-monotonic
taker = np.arange(len(mi), dtype=np.intp)
taker[::2] = taker[::-2]
mi2 = mi.take(taker)
loc5 = mi2.get_loc(key)
expected5 = np.zeros(len(mi2), dtype=bool)
expected5[[3, 14]] = True
tm.assert_numpy_array_equal(loc5, expected5)


def test_partial_string_timestamp_multiindex(df):
# GH10331
df_swap = df.swaplevel(0, 1).sort_index()
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,19 @@ def test_get_loc_datetime_index():
assert mi.get_loc("2001-01") == slice(0, 31, None)
assert index.get_loc("2001-01") == slice(0, 31, None)

loc = mi[::2].get_loc("2001-01")
expected = index[::2].get_loc("2001-01")
assert loc == expected

loc = mi.repeat(2).get_loc("2001-01")
expected = index.repeat(2).get_loc("2001-01")
assert loc == expected

loc = mi.append(mi).get_loc("2001-01")
expected = index.append(index).get_loc("2001-01")
# TODO: standardize return type for MultiIndex.get_loc
tm.assert_numpy_array_equal(loc.nonzero()[0], expected)


def test_loc_setitem_indexer_differently_ordered():
# GH#34603
Expand Down