Skip to content
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

BUG: Return KeyError for invalid string key #23540

Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ Indexing
^^^^^^^^

- The traceback from a ``KeyError`` when asking ``.loc`` for a single missing label is now shorter and more clear (:issue:`21557`)
- :class:`PeriodIndex` now emits a ``KeyError`` when a malformed string is looked up, which is consistent with the behavior of :class:`DateTimeIndex` (:issue:`22803`)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
- When ``.ix`` is asked for a missing integer label in a :class:`MultiIndex` with a first level of integer type, it now raises a ``KeyError``, consistently with the case of a flat :class:`Int64Index`, rather than falling back to positional indexing (:issue:`21593`)
- Bug in :meth:`DatetimeIndex.reindex` when reindexing a tz-naive and tz-aware :class:`DatetimeIndex` (:issue:`8306`)
- Bug in :meth:`Series.reindex` when reindexing an empty series with a ``datetime64[ns, tz]`` dtype (:issue:`20869`)
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pandas.core.indexes.datetimelike import (
DatelikeOps, DatetimeIndexOpsMixin, wrap_arithmetic_op
)
from pandas.core.tools.datetimes import parse_time_string
from pandas.core.tools.datetimes import parse_time_string, DateParseError

from pandas._libs import tslib, index as libindex
from pandas._libs.tslibs.period import (Period, IncompatibleFrequency,
Expand Down Expand Up @@ -580,7 +580,10 @@ def searchsorted(self, value, side='left', sorter=None):
raise IncompatibleFrequency(msg)
value = value.ordinal
elif isinstance(value, compat.string_types):
value = Period(value, freq=self.freq).ordinal
try:
value = Period(value, freq=self.freq).ordinal
except DateParseError:
raise KeyError("Cannot interpret '{}' as period".format(value))

return self._ndarray_values.searchsorted(value, side=side,
sorter=sorter)
Expand Down Expand Up @@ -711,6 +714,9 @@ def get_loc(self, key, method=None, tolerance=None):
key = asdt
except TypeError:
pass
except DateParseError:
# A string with invalid format
raise KeyError("Cannot interpret '{}' as period".format(key))

try:
key = Period(key, freq=self.freq)
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np
import pytest

from pandas._libs import tslibs
from pandas._libs.tslibs import period as libperiod
from pandas.compat import lrange

Expand Down Expand Up @@ -363,7 +362,9 @@ def test_get_loc(self):
assert idx0.get_loc(p2) == expected_idx1_p2
assert idx0.get_loc(str(p2)) == expected_idx1_p2

pytest.raises(tslibs.parsing.DateParseError, idx0.get_loc, 'foo')
tm.assert_raises_regex(KeyError,
"Cannot interpret 'foo' as period",
idx0.get_loc, 'foo')
pytest.raises(KeyError, idx0.get_loc, 1.1)
pytest.raises(TypeError, idx0.get_loc, idx0)

Expand All @@ -378,7 +379,9 @@ def test_get_loc(self):
assert idx1.get_loc(p2) == expected_idx1_p2
assert idx1.get_loc(str(p2)) == expected_idx1_p2

pytest.raises(tslibs.parsing.DateParseError, idx1.get_loc, 'foo')
tm.assert_raises_regex(KeyError,
"Cannot interpret 'foo' as period",
idx1.get_loc, 'foo')
pytest.raises(KeyError, idx1.get_loc, 1.1)
pytest.raises(TypeError, idx1.get_loc, idx1)

Expand Down