Skip to content

Commit 9ec1e25

Browse files
committed
BUG: PeriodEngine doesn't work (#29038)
1 parent f03ed62 commit 9ec1e25

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

pandas/core/indexes/period.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
is_float_dtype,
1818
is_integer,
1919
is_integer_dtype,
20+
is_period_arraylike,
2021
pandas_dtype,
2122
)
2223

@@ -704,36 +705,38 @@ def get_loc(self, key, method=None, tolerance=None):
704705
-------
705706
loc : int
706707
"""
708+
if is_integer(key):
709+
ordinal = key
710+
else:
711+
key = self._cast_to_period_object(key)
712+
ordinal = iNaT if key is NaT else key.ordinal
713+
707714
try:
708-
return self._engine.get_loc(key)
715+
if tolerance is not None:
716+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
717+
return self._int64index.get_loc(ordinal, method, tolerance)
709718
except KeyError:
710-
if is_integer(key):
711-
raise
719+
raise KeyError(key)
712720

721+
def _cast_to_period_object(self, key):
722+
if is_period_arraylike(key):
723+
raise TypeError("'{val}' is an invalid key".format(val=key))
724+
elif isinstance(key, str):
713725
try:
714726
asdt, parsed, reso = parse_time_string(key, self.freq)
715727
key = asdt
716-
except TypeError:
717-
pass
718728
except DateParseError:
719729
# A string with invalid format
720730
raise KeyError("Cannot interpret '{}' as period".format(key))
721731

722-
try:
723-
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:
730-
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)
732+
try:
733+
key = Period(key, freq=self.freq)
734+
except ValueError:
735+
# we cannot construct the Period
736+
# as we have an invalid type
737+
raise KeyError(key)
734738

735-
except KeyError:
736-
raise KeyError(key)
739+
return key
737740

738741
def _maybe_cast_slice_bound(self, label, side, kind):
739742
"""

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def test_memory_usage(self, indices):
332332
# RangeIndex, IntervalIndex
333333
# don't have engines
334334
if not isinstance(indices, (RangeIndex, IntervalIndex)):
335-
assert result2 > result
335+
assert result2 >= result
336336

337337
if indices.inferred_type == "object":
338338
assert result3 > result2

pandas/tests/indexes/period/test_indexing.py

+29
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,32 @@ 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 = r"PeriodIndex\(\['2016'\], dtype='period\[A-DEC\]', freq='A-DEC'\)"
697+
with pytest.raises(TypeError, match=msg):
698+
idx.get_loc(pi)
699+
700+
ordinal = Period(2015, freq="A").ordinal
701+
assert idx.get_loc(ordinal) == 1
702+
703+
with pytest.raises(KeyError, match="2015"):
704+
idx.get_loc(2015)
705+
706+
with pytest.raises(KeyError, match="Cannot interpret 'foo-bar' as period"):
707+
idx.get_loc("foo-bar")
708+
709+
assert idx.get_loc("2015-01-01") == 1
710+
711+
with pytest.raises(KeyError, match=r"\(2015, 2016\)"):
712+
idx.get_loc((2015, 2016))
713+
714+
assert idx.get_loc(datetime(2016, 1, 1)) == 2
715+
716+
assert idx.get_loc(np.datetime64("NaT")) == 4

0 commit comments

Comments
 (0)