Skip to content

Commit ee64ebc

Browse files
proostjreback
authored andcommitted
TST: loc misbehaves when Period is at start of 3-level MultiIndex (#28628)
1 parent 9826158 commit ee64ebc

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

pandas/tests/indexes/period/test_period.py

+38
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,44 @@ def test_insert(self):
649649
result = period_range("2017Q1", periods=4, freq="Q").insert(1, na)
650650
tm.assert_index_equal(result, expected)
651651

652+
@pytest.mark.parametrize(
653+
"msg, key",
654+
[
655+
(r"Period\('2019', 'A-DEC'\), 'foo', 'bar'", (Period(2019), "foo", "bar")),
656+
(r"Period\('2019', 'A-DEC'\), 'y1', 'bar'", (Period(2019), "y1", "bar")),
657+
(r"Period\('2019', 'A-DEC'\), 'foo', 'z1'", (Period(2019), "foo", "z1")),
658+
(
659+
r"Period\('2018', 'A-DEC'\), Period\('2016', 'A-DEC'\), 'bar'",
660+
(Period(2018), Period(2016), "bar"),
661+
),
662+
(r"Period\('2018', 'A-DEC'\), 'foo', 'y1'", (Period(2018), "foo", "y1")),
663+
(
664+
r"Period\('2017', 'A-DEC'\), 'foo', Period\('2015', 'A-DEC'\)",
665+
(Period(2017), "foo", Period(2015)),
666+
),
667+
(r"Period\('2017', 'A-DEC'\), 'z1', 'bar'", (Period(2017), "z1", "bar")),
668+
],
669+
)
670+
def test_contains_raise_error_if_period_index_is_in_multi_index(self, msg, key):
671+
# issue 20684
672+
"""
673+
parse_time_string return parameter if type not matched.
674+
PeriodIndex.get_loc takes returned value from parse_time_string as a tuple.
675+
If first argument is Period and a tuple has 3 items,
676+
process go on not raise exception
677+
"""
678+
df = DataFrame(
679+
{
680+
"A": [Period(2019), "x1", "x2"],
681+
"B": [Period(2018), Period(2016), "y1"],
682+
"C": [Period(2017), "z1", Period(2015)],
683+
"V1": [1, 2, 3],
684+
"V2": [10, 20, 30],
685+
}
686+
).set_index(["A", "B", "C"])
687+
with pytest.raises(KeyError, match=msg):
688+
df.loc[key]
689+
652690

653691
def test_maybe_convert_timedelta():
654692
pi = PeriodIndex(["2000", "2001"], freq="D")

pandas/tests/tslibs/test_parsing.py

+10
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,13 @@ def test_try_parse_dates():
215215

216216
expected = np.array([parse(d, dayfirst=True) for d in arr])
217217
tm.assert_numpy_array_equal(result, expected)
218+
219+
220+
def test_parse_time_string_check_instance_type_raise_exception():
221+
# issue 20684
222+
with pytest.raises(TypeError):
223+
parse_time_string((1, 2, 3))
224+
225+
result = parse_time_string("2019")
226+
expected = (datetime(2019, 1, 1), datetime(2019, 1, 1), "year")
227+
assert result == expected

0 commit comments

Comments
 (0)