Skip to content

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

Closed
@simonjayhawkins

Description

@simonjayhawkins
  • 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]

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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions