Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5baf8eb

Browse files
committedAug 4, 2024·
Index should be ignored when using the on keyword argument to resample
1 parent 642d244 commit 5baf8eb

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
 

‎doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ These improvements also fixed certain bugs in groupby:
117117
- :meth:`.DataFrameGroupBy.agg` would fail when there are multiple groupings, unobserved groups, and ``as_index=False`` (:issue:`36698`)
118118
- :meth:`.DataFrameGroupBy.groups` with ``sort=False`` would sort groups; they now occur in the order they are observed (:issue:`56966`)
119119
- :meth:`.DataFrameGroupBy.nunique` would fail when there are multiple groupings, unobserved groups, and ``as_index=False`` (:issue:`52848`)
120+
- :meth:`.DataFrameGroupBy.resample` with an ``on`` value that is not ``None`` would have incorrect values when the index is out of order (:issue:`59350`)
120121
- :meth:`.DataFrameGroupBy.sum` would have incorrect values when there are multiple groupings, unobserved groups, and non-numeric data (:issue:`43891`)
121122
- :meth:`.DataFrameGroupBy.value_counts` would produce incorrect results when used with some categorical and some non-categorical groupings and ``observed=False`` (:issue:`56016`)
122123

‎pandas/core/resample.py

+3
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,9 @@ def get_resampler_for_grouping(
19271927
"""
19281928
# .resample uses 'on' similar to how .groupby uses 'key'
19291929
tg = TimeGrouper(freq=rule, key=on, **kwargs)
1930+
# GH 59350: Index should be ignored when using the on keyword argument to resample
1931+
if on is not None:
1932+
groupby.obj = groupby.obj.reset_index(drop=True)
19301933
resampler = tg._get_resampler(groupby.obj)
19311934
return resampler._get_resampler_for_grouping(
19321935
groupby=groupby, include_groups=include_groups, key=tg.key

‎pandas/tests/resample/test_resampler_grouper.py

+33
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,36 @@ def test_groupby_resample_on_index_with_list_of_keys_missing_column():
689689
rs = gb.resample("2D")
690690
with pytest.raises(KeyError, match="Columns not found"):
691691
rs[["val_not_in_dataframe"]]
692+
693+
694+
def test_groupby_resample_when_index_is_out_of_order():
695+
# GH 59350
696+
df = DataFrame(
697+
data={
698+
"datetime": [
699+
pd.to_datetime("2024-07-30T00:00Z"),
700+
pd.to_datetime("2024-07-30T00:01Z"),
701+
],
702+
"group": ["A", "A"],
703+
"numbers": [100, 200],
704+
},
705+
index=[1, 0],
706+
)
707+
708+
gb = df.groupby("group")
709+
rs = gb.resample("1min", on="datetime")
710+
result = rs.aggregate({"numbers": "sum"})
711+
712+
index = pd.MultiIndex.from_arrays(
713+
[
714+
["A", "A"],
715+
[pd.to_datetime("2024-07-30T00:00Z"), pd.to_datetime("2024-07-30T00:01Z")],
716+
],
717+
names=[
718+
"group",
719+
"datetime",
720+
],
721+
)
722+
expected = DataFrame({"numbers": [100, 200]}, index=index)
723+
724+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)
Please sign in to comment.