Skip to content

TST: loc misbehaves when Period is at start of 3-level MultiIndex #28628

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 1 commit into from
Oct 30, 2019
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
6 changes: 2 additions & 4 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,8 @@ def parse_time_string(arg, freq=None, dayfirst=None, yearfirst=None):
-------
datetime, datetime/dateutil.parser._result, str
"""
if not isinstance(arg, (str, unicode)):
# Note: cython recognizes `unicode` in both py2/py3, optimizes
# this check into a C call.
return arg
if not isinstance(arg, str):
raise TypeError("parse_time_string argument must be str")

if getattr(freq, "_typ", None) == "dateoffset":
freq = freq.rule_code
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,44 @@ def test_insert(self):
result = period_range("2017Q1", periods=4, freq="Q").insert(1, na)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"msg, key",
[
(r"Period\('2019', 'A-DEC'\), 'foo', 'bar'", (Period(2019), "foo", "bar")),
(r"Period\('2019', 'A-DEC'\), 'y1', 'bar'", (Period(2019), "y1", "bar")),
(r"Period\('2019', 'A-DEC'\), 'foo', 'z1'", (Period(2019), "foo", "z1")),
(
r"Period\('2018', 'A-DEC'\), Period\('2016', 'A-DEC'\), 'bar'",
(Period(2018), Period(2016), "bar"),
),
(r"Period\('2018', 'A-DEC'\), 'foo', 'y1'", (Period(2018), "foo", "y1")),
(
r"Period\('2017', 'A-DEC'\), 'foo', Period\('2015', 'A-DEC'\)",
(Period(2017), "foo", Period(2015)),
),
(r"Period\('2017', 'A-DEC'\), 'z1', 'bar'", (Period(2017), "z1", "bar")),
],
)
def test_contains_raise_error_if_period_index_is_in_multi_index(self, msg, key):
# issue 20684
"""
parse_time_string return parameter if type not matched.
PeriodIndex.get_loc takes returned value from parse_time_string as a tuple.
If first argument is Period and a tuple has 3 items,
process go on not raise exception
"""
df = DataFrame(
{
"A": [Period(2019), "x1", "x2"],
"B": [Period(2018), Period(2016), "y1"],
"C": [Period(2017), "z1", Period(2015)],
"V1": [1, 2, 3],
"V2": [10, 20, 30],
}
).set_index(["A", "B", "C"])
with pytest.raises(KeyError, match=msg):
df.loc[key]


def test_maybe_convert_timedelta():
pi = PeriodIndex(["2000", "2001"], freq="D")
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,13 @@ def test_try_parse_dates():

expected = np.array([parse(d, dayfirst=True) for d in arr])
tm.assert_numpy_array_equal(result, expected)


def test_parse_time_string_check_instance_type_raise_exception():
# issue 20684
with pytest.raises(TypeError):
parse_time_string((1, 2, 3))

result = parse_time_string("2019")
expected = (datetime(2019, 1, 1), datetime(2019, 1, 1), "year")
assert result == expected