Skip to content

BUG: Regression in PeriodIndex.loc string slicing behaviour between 1.0.x and >= 1.1.0 #40145

Open
@enisnazif

Description

@enisnazif
  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
print(pd.__version__)
idx = pd.PeriodIndex(['2020-01-01 11:00', '2020-01-01 12:00'], freq='H')
df = pd.DataFrame([1,2], index=idx)
df.loc['2020-01-01 11:00']

Problem description

It seems there has been a change in the behaviour of string slicing on a PeriodIndex in Pandas 1.1 onwards. Despite the exact value being present in the index, a KeyError is returned.

Output in 0.25.3:

0    1
Name: 2020-01-01 11:00, dtype: int64

Output in 1.0.1

0    1
Name: 2020-01-01 11:00, dtype: int6

Output in versions >= 1.1.0

KeyError: '2020-01-01 11:00'

Interestingly,

df.loc['2020-01-01 11']

still works and gives the expected answer.

Looks to be due to this assertion:

if not grp.value < freqn:

Expected Output

0    1
Name: 2020-01-01 11:00, dtype: int6

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.7.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.128-microsoft-standard
Version : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.2
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.22
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : 1.3.15
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesPeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions