Skip to content

Commit f8bcf6d

Browse files
committed
BUG: PeriodEngine doesn't work (pandas-dev#29038)
1 parent f03ed62 commit f8bcf6d

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

pandas/core/indexes/period.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -704,36 +704,33 @@ def get_loc(self, key, method=None, tolerance=None):
704704
-------
705705
loc : int
706706
"""
707-
try:
708-
return self._engine.get_loc(key)
709-
except KeyError:
710-
if is_integer(key):
711-
raise
712-
713-
try:
714-
asdt, parsed, reso = parse_time_string(key, self.freq)
715-
key = asdt
716-
except TypeError:
717-
pass
718-
except DateParseError:
719-
# A string with invalid format
720-
raise KeyError("Cannot interpret '{}' as period".format(key))
707+
if is_integer(key):
708+
ordinal = key
709+
else:
710+
if isinstance(key, str):
711+
try:
712+
asdt, parsed, reso = parse_time_string(key, self.freq)
713+
key = asdt
714+
except DateParseError:
715+
# A string with invalid format
716+
raise KeyError("Cannot interpret '{}' as period".format(key))
721717

722718
try:
723719
key = Period(key, freq=self.freq)
724-
except ValueError:
725-
# we cannot construct the Period
726-
# as we have an invalid type
727-
raise KeyError(key)
728-
729-
try:
730720
ordinal = iNaT if key is NaT else key.ordinal
731-
if tolerance is not None:
732-
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
733-
return self._int64index.get_loc(ordinal, method, tolerance)
734-
735-
except KeyError:
736-
raise KeyError(key)
721+
except ValueError:
722+
# we cannot construct the Period as we have an invalid type
723+
if is_float(key):
724+
# Because of conflicts with test case
725+
raise KeyError(key)
726+
else:
727+
raise TypeError("'{val}' is an invalid key".format(val=key))
728+
try:
729+
if tolerance is not None:
730+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
731+
return self._int64index.get_loc(ordinal, method, tolerance)
732+
except KeyError:
733+
raise KeyError(key)
737734

738735
def _maybe_cast_slice_bound(self, label, side, kind):
739736
"""

pandas/tests/indexes/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_memory_usage(self, indices):
331331

332332
# RangeIndex, IntervalIndex
333333
# don't have engines
334-
if not isinstance(indices, (RangeIndex, IntervalIndex)):
334+
if not isinstance(indices, (RangeIndex, IntervalIndex, PeriodIndex)):
335335
assert result2 > result
336336

337337
if indices.inferred_type == "object":

pandas/tests/indexes/period/test_indexing.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,35 @@ def test_period_index_indexer(self):
685685
tm.assert_frame_equal(df, df.loc[list(idx)])
686686
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
687687
tm.assert_frame_equal(df, df.loc[list(idx)])
688+
689+
def test_get_loc_not_use_engine(self):
690+
# issue 29234
691+
idx = PeriodIndex(
692+
["2014", "2015", "2016", "2017", np.datetime64("NaT")], freq="A"
693+
)
694+
695+
pi = PeriodIndex(["2016"], freq="A")
696+
msg = (
697+
r"'PeriodIndex\(\['2016'\], dtype='period\[A-DEC\]', freq='A-DEC'\)'"
698+
r" is an invalid key"
699+
)
700+
with pytest.raises(TypeError, match=msg):
701+
idx.get_loc(pi)
702+
703+
ordinal = Period(2015, freq="A").ordinal
704+
assert idx.get_loc(ordinal) == 1
705+
706+
with pytest.raises(KeyError, match="2015"):
707+
idx.get_loc(2015)
708+
709+
with pytest.raises(KeyError, match="Cannot interpret 'foo-bar' as period"):
710+
idx.get_loc("foo-bar")
711+
712+
assert idx.get_loc("2015-01-01") == 1
713+
714+
with pytest.raises(TypeError, match=r"'\(2015, 2016\)' is an invalid key"):
715+
idx.get_loc((2015, 2016))
716+
717+
assert idx.get_loc(datetime(2016, 1, 1)) == 2
718+
719+
assert idx.get_loc(np.datetime64("NaT")) == 4

0 commit comments

Comments
 (0)