Skip to content

REGR: string indexing on PeriodIndex raises KeyError on master (not yet released) #33964

@simonjayhawkins

Description

@simonjayhawkins
Member
  • I have checked that this issue has not already been reported.

    I have confirmed this bug exists on the latest version of pandas.

    (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

>>> import pandas as pd
>>>
>>> pd.__version__
'1.1.0.dev0+1466.ga3477c769'
>>>
>>> index = pd.period_range(start="2000", periods=20, freq="B")
>>> series = pd.Series(range(20), index=index)
>>>
>>> series.index
PeriodIndex(['2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06',
             '2000-01-07', '2000-01-10', '2000-01-11', '2000-01-12',
             '2000-01-13', '2000-01-14', '2000-01-17', '2000-01-18',
             '2000-01-19', '2000-01-20', '2000-01-21', '2000-01-24',
             '2000-01-25', '2000-01-26', '2000-01-27', '2000-01-28'],
            dtype='period[B]', freq='B')
>>>
>>> series.loc["2000-01-14"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\pandas\pandas\core\indexing.py", line 871, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Users\simon\pandas\pandas\core\indexing.py", line 1102, in _getitem_axis
    return self._get_label(key, axis=axis)
  File "C:\Users\simon\pandas\pandas\core\indexing.py", line 1051, in _get_label
    return self.obj.xs(label, axis=axis)
  File "C:\Users\simon\pandas\pandas\core\generic.py", line 3512, in xs
    loc = self.index.get_loc(key)
  File "C:\Users\simon\pandas\pandas\core\indexes\period.py", line 519, in get_loc
    raise KeyError(key)
KeyError: '2000-01-14'
>>>

Problem description

regression in #31172 (i.e. not yet released)

9a211aa is the first bad commit
commit 9a211aa
Author: jbrockmendel jbrockmendel@gmail.com
Date: Sat Jan 25 08:07:15 2020 -0800

BUG: inconsistency between PeriodIndex.get_value vs get_loc (#31172)

cc @jbrockmendel

Expected Output

>>> import pandas as pd
>>>
>>> pd.__version__
'1.0.3'
>>>
>>> index = pd.period_range(start="2000", periods=20, freq="B")
>>> series = pd.Series(range(20), index=index)
>>>
>>> series.index
PeriodIndex(['2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06',
             '2000-01-07', '2000-01-10', '2000-01-11', '2000-01-12',
             '2000-01-13', '2000-01-14', '2000-01-17', '2000-01-18',
             '2000-01-19', '2000-01-20', '2000-01-21', '2000-01-24',
             '2000-01-25', '2000-01-26', '2000-01-27', '2000-01-28'],
            dtype='period[B]', freq='B')
>>>
>>> series.loc["2000-01-14"]
9
>>>

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

Activity

added
IndexingRelated to indexing on series/frames, not to indexes themselves
RegressionFunctionality that used to work in a prior pandas version
PeriodPeriod data type
on May 4, 2020
added this to the 1.1 milestone on May 4, 2020
added
BlockerBlocking issue or pull request for an upcoming release
on May 19, 2020
TomAugspurger

TomAugspurger commented on Jun 12, 2020

@TomAugspurger
Contributor

@jbrockmendel do you have time to look into this in the next week or two? If not I can take a look next week.

jbrockmendel

jbrockmendel commented on Jun 12, 2020

@jbrockmendel
Member

I'm taking a break for a little while, can look at this when i get back

TomAugspurger

TomAugspurger commented on Jun 12, 2020

@TomAugspurger
Contributor

No worries. Enjoy the break.

added a commit that references this issue on Jun 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BlockerBlocking issue or pull request for an upcoming releaseBugIndexingRelated to indexing on series/frames, not to indexes themselvesPeriodPeriod data typeRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @TomAugspurger@jbrockmendel@mroeschke@simonjayhawkins

      Issue actions

        REGR: string indexing on PeriodIndex raises KeyError on master (not yet released) · Issue #33964 · pandas-dev/pandas