Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [X] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame(index=pd.date_range(start="2014-01-01", end="2023-01-01", freq="MS"))
df.resample("QS").sum().loc["2014-Q1"]

Issue Description

The example above throws a key error. Interestingly, df.resample("QS").sum().loc["2015-Q1"] yields the indices corresponding to the first quarter of 2014.

See stackoverflow issue.

https://stackoverflow.com/questions/78315553/pandas-bug-when-trying-to-loc-data-on-a-quarter/78320367?

Expected Behavior

The view should be on the first quarter of 2014. Expected output is

Empty DataFrame
Columns: []
Index: [2014-01-01 00:00:00]

but it doesn't find it. Instead, I get the following with another indexing

In [12]: df_resampled.loc["2015-Q1"]
Out[12]:
Empty DataFrame
Columns: []
Index: [2014-01-01 00:00:00]

Note that the problem disappears if I do

df_resampled.freq = None
df_resampled.loc["2014-Q1"]

In which case the correct output is yielded.

Installed Versions

INSTALLED VERSIONS ------------------ commit : bdc79c146c2e32f2cab629be240f01658cfb6cc2 python : 3.12.2.final.0 python-bits : 64 OS : Darwin OS-release : 23.4.0 Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8 pandas : 2.2.1 numpy : 1.26.4 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 68.2.2 pip : 23.3.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : 8.20.0 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None bottleneck : 1.3.7 dataframe-api-compat : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : 2.8.7 odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None

Comment From: rhshadrach

Thanks for the report. The result of resample has freq as <QuarterBegin: startingMonth=1>. With this on the index, the lookup for "2014-Q1" is looking for Timestamp('2013-02-01 00:00:00') which seems off to me.

It doesn't appear to me that resample is the issue.

cc @MarcoGorelli

Comment From: MarcoGorelli

thanks for the ping, adding this to my mental list of quarter oddities https://github.com/pandas-dev/pandas/issues/8435#issuecomment-2012433875

Comment From: tuhinsharma121

anyone working on it? Can I give it a try?

Comment From: tuhinsharma121

take