Skip to content

TST: indexing tests for #21168, #27420, #15928, #30053 #37228

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 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 17 additions & 1 deletion pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

from pandas.errors import InvalidIndexError
from pandas.errors import InvalidIndexError, PerformanceWarning

import pandas as pd
from pandas import Categorical, Index, MultiIndex, date_range
Expand Down Expand Up @@ -646,6 +646,22 @@ def test_get_loc_duplicates2(self):

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

def test_get_loc_past_lexsort_depth(self):
# GH#30053
idx = MultiIndex(
levels=[["a"], [0, 7], [1]],
codes=[[0, 0], [1, 0], [0, 0]],
names=["x", "y", "z"],
sortorder=0,
)
key = ("a", 7)

with tm.assert_produces_warning(PerformanceWarning):
# PerformanceWarning: indexing past lexsort depth may impact performance
result = idx.get_loc(key)

assert result == slice(0, 1, None)


class TestWhere:
def test_where(self):
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,47 @@ def test_loc_with_mi_indexer():
columns=["author", "price"],
)
tm.assert_frame_equal(result, expected)


def test_getitem_str_slice(datapath):
# GH#15928
path = datapath("reshape", "merge", "data", "quotes2.csv")
df = pd.read_csv(path, parse_dates=["time"])
df2 = df.set_index(["ticker", "time"]).sort_index()

res = df2.loc[("AAPL", slice("2016-05-25 13:30:00")), :].droplevel(0)
expected = df2.loc["AAPL"].loc[slice("2016-05-25 13:30:00"), :]
tm.assert_frame_equal(res, expected)


def test_3levels_leading_period_index():
# GH#24091
pi = pd.PeriodIndex(
["20181101 1100", "20181101 1200", "20181102 1300", "20181102 1400"],
name="datetime",
freq="B",
)
lev2 = ["A", "A", "Z", "W"]
lev3 = ["B", "C", "Q", "F"]
mi = pd.MultiIndex.from_arrays([pi, lev2, lev3])

ser = pd.Series(range(4), index=mi, dtype=np.float64)
result = ser.loc[(pi[0], "A", "B")]
assert result == 0.0


class TestKeyErrorsWithMultiIndex:
def test_missing_keys_raises_keyerror(self):
# GH#27420 KeyError, not TypeError
df = pd.DataFrame(np.arange(12).reshape(4, 3), columns=["A", "B", "C"])
df2 = df.set_index(["A", "B"])

with pytest.raises(KeyError, match="1"):
df2.loc[(1, 6)]

def test_missing_key_raises_keyerror2(self):
# GH#21168 KeyError, not "IndexingError: Too many indexers"
ser = pd.Series(-1, index=pd.MultiIndex.from_product([[0, 1]] * 2))

with pytest.raises(KeyError, match=r"\(0, 3\)"):
ser.loc[0, 3]