BUG: `groupby` then `resample` on column gives incorrect results if the index is out of order
State: closed

- [x] closes #59350
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Labels:
name: Bug
name: Groupby
name: Resample
description: resample method
name: Stale

Comments:
This PR looks good to me. I don't think the CI failures are related so @aram-cinnamon if you merge in the main branch will likely fix those.

@rhshadrach @mroeschke any comments?
<details>
<summary>Removed</summary>

I think the example below "works" on main (i.e. doesn't raise - but it still gives incorrect results as in the issue), but would raise if we merge this PR.

```python
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=pd.Index([1, 0], name="datetime"))

result = df.groupby('group').resample('1min', on='datetime').aggregate(dict(value='sum'))
print(result)
```

@aram-cinnamon - can you confirm?

</details>

Ignore me  - I missed the `drop=True` in the reset_index.
> [aram-cinnamon](https://github.com/aram-cinnamon) [force-pushed](https://github.com/pandas-dev/pandas/compare/70e71f76692e7c1e0ea429a6c94de99f632611b2..55fe96bc2ab4a69d3486d97b790b7ff41a95245c) the groupby-then-resample-if-index-out-of-order branch from[ 70e71f7 ](https://github.com/pandas-dev/pandas/commit/70e71f76692e7c1e0ea429a6c94de99f632611b2)to 55fe96b

@aram-cinnamon - not sure if you're aware, but once a review has been done, force-pushing means that reviewers need to review the entire PR again rather than being able to look at your subsequent changes.
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.
@mroeschke Hey sorry, I'm coming back to this after a break. Can you please reopen? I merged in the main branch, and I responded to a review comment.
LGTM! This looks like a good fix for the issue described in #59350.

The changes in `pandas/core/groupby/grouper.py` directly address the scenario where an out-of-order index could cause misalignment when resampling on a column. The added tests in `pandas/tests/resample/test_resampler_grouper.py` provide good coverage for the fix and related cases. The whatsnew entry is also clear.

One minor suggestion: consider adding a brief inline comment in `_set_grouper` explaining *why* `ax = self._grouper` is the correct assignment when `obj.index` is not a `RangeIndex` and `self._indexer` is `None` in that specific branch. This would help future readers understand the logic without needing to refer back to the PR/issue. Because it might be tricky to read

Example comment:
```python
                elif isinstance(obj.index, RangeIndex):
                    # Standard case for RangeIndex
                    ax = self._grouper.take(obj.index)
                else:
                    # GH Issue 59350: If index is not RangeIndex and not sorted here,
                    # avoid re-taking based on potentially mis-ordered obj.index.
                    # self._grouper should already align with obj's values via key.
                    ax = self._grouper
```

Apart from that minor documentation point, I personally don't see anything that can be improved.
Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.