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: Incorrect behaviour when using loc on a resampled time index dataframe #58255

Open
3 tasks done
jose-moran opened this issue Apr 14, 2024 · 4 comments
Open
3 tasks done
Assignees
Labels
Bug Frequency DateOffsets Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@jose-moran
Copy link

jose-moran commented Apr 14, 2024

Pandas version checks

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

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

  • 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 : bdc79c1
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

@jose-moran jose-moran added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 14, 2024
@rhshadrach
Copy link
Member

rhshadrach commented Apr 14, 2024

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

@rhshadrach rhshadrach added Frequency DateOffsets Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 14, 2024
@MarcoGorelli
Copy link
Member

thanks for the ping, adding this to my mental list of quarter oddities #8435 (comment)

@tuhinsharma121
Copy link
Contributor

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

@tuhinsharma121
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Frequency DateOffsets Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

4 participants