Skip to content

Commit

Permalink
BUG: Return KeyError for invalid string key (pandas-dev#23540)
Browse files Browse the repository at this point in the history
  • Loading branch information
toobaz authored and JustinZhengBC committed Nov 14, 2018
1 parent 574a03a commit 73cc01b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Expand Up @@ -1206,6 +1206,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`)
- 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
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
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

0 comments on commit 73cc01b

Please sign in to comment.