Description
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(dict(
datetime=[pd.to_datetime('2024-07-30T00:00Z'), pd.to_datetime('2024-07-30T00:01Z')],
group=['A', 'A'],
value=[100, 200],
), index=[1, 0])
df.groupby('group').resample('1min', on='datetime').aggregate(dict(value='sum'))
Issue Description
The example above gives the following incorrect output:
value
group datetime
A 2024-07-30 00:00:00+00:00 200
2024-07-30 00:01:00+00:00 100
Expected Behavior
The correct output is:
value
group datetime
A 2024-07-30 00:00:00+00:00 100
2024-07-30 00:01:00+00:00 200
The correct output can be got using either of the following - either reset the index, or use the datetime column as the index.
df.reset_index().groupby('group').resample('1min', on='datetime').aggregate(dict(value='sum'))
df.set_index('datetime').groupby('group').resample('1min').aggregate(dict(value='sum'))
It seems the out-of-order index ([1, 0]
instead of [0, 1]
) is affecting the resampling, even though the index should be ignored when using the on
keyword argument to resample
. This may be related to #35275 where it seems the index also affects the operation of resampling on a column (in that case giving an IndexError if the index value is not less than the length of the data frame.)
Installed Versions
INSTALLED VERSIONS
commit : d9cdd2e
python : 3.9.15.final.0
python-bits : 64
OS : Linux
OS-release : 5.14.0-284.11.1.el9_2.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Tue May 9 17:09:15 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 70.3.0
pip : 24.0
Cython : 3.0.10
pytest : 8.2.2
hypothesis : 6.108.2
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.1
html5lib : None
pymysql : 1.4.6
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.18.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : 2024.5.0
fsspec : 2024.6.1
gcsfs : None
matplotlib : 3.9.1
numba : 0.60.0
numexpr : 2.10.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.1
sqlalchemy : None
tables : 3.9.2
tabulate : 0.9.0
xarray : 2024.3.0
xlrd : 2.0.1
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None