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: inconsistency between PeriodIndex.get_value vs get_loc #31172

Merged
merged 6 commits into from Jan 25, 2020

Conversation

jbrockmendel
Copy link
Member

ATM we have inconsistent treatment for indexing with strings where the string has a higher resolution than the PeriodIndex. Luckily only one test checks for the inconsistent PeriodIndex.__contains__ behavior, so fixing it isn't too invasive.

pi = pd.period_range("2017-09-01", freq="D", periods=3)
ser = pd.Series(range(5, 8), index=pi)
key = "2017-09-01 00:00:01"

# master
>>> key in pi
True
>>> pi.get_loc(key)
0
>>> pi.get_value(ser, key)
KeyError: '2017-09-01 00:00:01'

# PR
>>> key in pi
False
>>> pi.get_loc(key)
KeyError: '2017-09-01 00:00:01'
>>> pi.get_value(ser, key)
KeyError: '2017-09-01 00:00:01'

Note: the diff is fairly big because we're mirroring the code in get_value. Once this is fixed, we'll have get_value dispatch to get_loc and remove basically the same amount of code as we're adding here.

@gfyoung gfyoung added Bug Period Period data type labels Jan 21, 2020
Copy link
Member

@gfyoung gfyoung left a comment

Choose a reason for hiding this comment

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

Once this is fixed, we'll have get_value dispatch to get_loc and remove basically the same amount of code as we're adding here.

Sounds like a good follow-up once this is merged.

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

pandas/core/indexes/period.py Show resolved Hide resolved
@jbrockmendel
Copy link
Member Author

updated+green

@jreback jreback added this to the 1.1 milestone Jan 25, 2020
@jreback jreback merged commit 9a211aa into pandas-dev:master Jan 25, 2020
@jreback
Copy link
Contributor

jreback commented Jan 25, 2020

thanks

@jbrockmendel jbrockmendel deleted the bug-pi-get_loc2 branch January 25, 2020 16:34
keechongtan added a commit to keechongtan/pandas that referenced this pull request Jan 27, 2020
…ndexing-1row-df

* upstream/master: (194 commits)
  DOC Remove Python 2 specific comments from documentation (pandas-dev#31198)
  Follow up PR: pandas-dev#28097 Simplify branch statement (pandas-dev#29243)
  BUG: DatetimeIndex.snap incorrectly setting freq (pandas-dev#31188)
  Move DataFrame.info() to live with similar functions (pandas-dev#31317)
  ENH: accept a dictionary in plot colors (pandas-dev#31071)
  PERF: add shortcut to Timestamp constructor (pandas-dev#30676)
  CLN/MAINT: Clean and annotate stata reader and writers (pandas-dev#31072)
  REF: define _get_slice_axis in correct classes (pandas-dev#31304)
  BUG: DataFrame.floordiv(ser, axis=0) not matching column-wise bheavior (pandas-dev#31271)
  PERF: optimize is_scalar, is_iterator (pandas-dev#31294)
  BUG: Series rolling count ignores min_periods (pandas-dev#30923)
  xfail sparse warning; closes pandas-dev#31310 (pandas-dev#31311)
  REF: DatetimeIndex.get_value wrap DTI.get_loc (pandas-dev#31314)
  CLN: internals.managers (pandas-dev#31316)
  PERF: avoid copies if possible in fill_binop (pandas-dev#31300)
  Add test for multiindex json (pandas-dev#31307)
  BUG: passing TDA and wrong freq to TimedeltaIndex (pandas-dev#31268)
  BUG: inconsistency between PeriodIndex.get_value vs get_loc (pandas-dev#31172)
  CLN: remove _set_subtyp (pandas-dev#31301)
  CI: Updated version of macos image (pandas-dev#31292)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Period Period data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants