Skip to content

Commit c908355

Browse files
authored
TST: indexing tests for #21168, #27420, #15928, #30053 (#37228)
* TST: tests for #21168, #27420, #15928, #30053 * use datapath
1 parent 01cf2f2 commit c908355

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-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

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

0 commit comments

Comments
 (0)