Skip to content

BUG: inconsistency between PeriodIndex.get_value vs get_loc #31172

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 6 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
BUG: Inconsistency between PeriodIndex.get_value vs get_loc
  • Loading branch information
jbrockmendel committed Jan 21, 2020
commit 9f4735371f5c51edcacdb33656aa3117c366db9b
31 changes: 26 additions & 5 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,33 @@ def get_loc(self, key, method=None, tolerance=None):
"""

if isinstance(key, str):

try:
asdt, reso = parse_time_string(key, self.freq)
key = asdt
except DateParseError:
# A string with invalid format
raise KeyError(f"Cannot interpret '{key}' as period")
loc = self._get_string_slice(key)
return loc
except (TypeError, ValueError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OverflowError can no longer occur?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill see if i can cook up a test case that overflows


try:
asdt, reso = parse_time_string(key, self.freq)
except DateParseError:
# A string with invalid format
raise KeyError(f"Cannot interpret '{key}' as period")

grp = resolution.Resolution.get_freq_group(reso)
freqn = resolution.get_freq_group(self.freq)

# _get_string_slice will handle cases where grp < freqn
assert grp >= freqn

if grp == freqn:
key = Period(asdt, freq=self.freq)
loc = self.get_loc(key, method=method, tolerance=tolerance)
# TODO: or better just to let fall through?
return loc
elif method is None:
raise KeyError(key)
else:
key = asdt

elif is_integer(key):
# Period constructor will cast to string, which we dont want
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,19 @@ def test_contains(self):

ps0 = [p0, p1, p2]
idx0 = pd.PeriodIndex(ps0)
ser = pd.Series(range(6, 9), index=idx0)

for p in ps0:
assert p in idx0
assert str(p) in idx0

assert "2017-09-01 00:00:01" in idx0
# Higher-resolution period-like are _not_ considered as contained
key = "2017-09-01 00:00:01"
assert key not in idx0
with pytest.raises(KeyError, match=key):
idx0.get_loc(key)
with pytest.raises(KeyError, match=key):
idx0.get_value(ser, key)

assert "2017-09" in idx0

Expand Down