Skip to content

Commit 9264f10

Browse files
committed
1 parent 54fa3da commit 9264f10

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

pandas/tests/indexes/multi/test_indexing.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from pandas.errors import InvalidIndexError
6+
from pandas.errors import InvalidIndexError, PerformanceWarning
77

88
import pandas as pd
99
from pandas import Categorical, Index, MultiIndex, date_range
@@ -646,6 +646,22 @@ def test_get_loc_duplicates2(self):
646646

647647
assert index.get_loc("D") == slice(0, 3)
648648

649+
def test_get_loc_past_lexsort_depth(self):
650+
# GH#30053
651+
idx = MultiIndex(
652+
levels=[["a"], [0, 7], [1]],
653+
codes=[[0, 0], [1, 0], [0, 0]],
654+
names=["x", "y", "z"],
655+
sortorder=0,
656+
)
657+
key = ("a", 7)
658+
659+
with tm.assert_produces_warning(PerformanceWarning):
660+
# PerformanceWarning: indexing past lexsort depth may impact performance
661+
result = idx.get_loc(key)
662+
663+
assert result == slice(0, 1, None)
664+
649665

650666
class TestWhere:
651667
def test_where(self):

pandas/tests/indexing/multiindex/test_loc.py

+48
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import numpy as np
24
import pytest
35

@@ -522,3 +524,49 @@ def test_loc_with_mi_indexer():
522524
columns=["author", "price"],
523525
)
524526
tm.assert_frame_equal(result, expected)
527+
528+
529+
def test_getitem_str_slice():
530+
# GH#15928
531+
path = os.path.join(
532+
pd.__path__[0], "tests", "reshape", "merge", "data", "quotes2.csv"
533+
)
534+
df = pd.read_csv(path, parse_dates=["time"])
535+
df2 = df.set_index(["ticker", "time"]).sort_index()
536+
537+
res = df2.loc[("AAPL", slice("2016-05-25 13:30:00")), :].droplevel(0)
538+
expected = df2.loc["AAPL"].loc[slice("2016-05-25 13:30:00"), :]
539+
tm.assert_frame_equal(res, expected)
540+
541+
542+
def test_3levels_leading_period_index():
543+
# GH#24091
544+
pi = pd.PeriodIndex(
545+
["20181101 1100", "20181101 1200", "20181102 1300", "20181102 1400"],
546+
name="datetime",
547+
freq="B",
548+
)
549+
lev2 = ["A", "A", "Z", "W"]
550+
lev3 = ["B", "C", "Q", "F"]
551+
mi = pd.MultiIndex.from_arrays([pi, lev2, lev3])
552+
553+
ser = pd.Series(range(4), index=mi, dtype=np.float64)
554+
result = ser.loc[(pi[0], "A", "B")]
555+
assert result == 0.0
556+
557+
558+
class TestKeyErrorsWithMultiIndex:
559+
def test_missing_keys_raises_keyerror(self):
560+
# GH#27420 KeyError, not TypeError
561+
df = pd.DataFrame(np.arange(12).reshape(4, 3), columns=["A", "B", "C"])
562+
df2 = df.set_index(["A", "B"])
563+
564+
with pytest.raises(KeyError, match="1"):
565+
df2.loc[(1, 6)]
566+
567+
def test_missing_key_raises_keyerror2(self):
568+
# GH#21168 KeyError, not "IndexingError: Too many indexers"
569+
ser = pd.Series(-1, index=pd.MultiIndex.from_product([[0, 1]] * 2))
570+
571+
with pytest.raises(KeyError, match=r"\(0, 3\)"):
572+
ser.loc[0, 3]

0 commit comments

Comments
 (0)