Skip to content

Commit 2708355

Browse files
mroeschkeMatt Roeschke
authored and
Kevin D Smith
committed
BUG: RollingGroupby not respecting sort=False (pandas-dev#36911)
* BUG: RollingGroupby not respecting sort * move to 1.1.4 * Just use factorize * Add comments and turn into dict comprenehsion Co-authored-by: Matt Roeschke <[email protected]>
1 parent 8f4a26b commit 2708355

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression where attempting to mutate a :class:`DateOffset` object would no longer raise an ``AttributeError`` (:issue:`36940`)
18+
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)
1819

1920
.. ---------------------------------------------------------------------------
2021

pandas/core/groupby/grouper.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,13 @@ def indices(self):
563563
if isinstance(self.grouper, ops.BaseGrouper):
564564
return self.grouper.indices
565565

566-
values = Categorical(self.grouper)
567-
return values._reverse_indexer()
566+
# Return a dictionary of {group label: [indices belonging to the group label]}
567+
# respecting whether sort was specified
568+
codes, uniques = algorithms.factorize(self.grouper, sort=self.sort)
569+
return {
570+
category: np.flatnonzero(codes == i)
571+
for i, category in enumerate(Index(uniques))
572+
}
568573

569574
@property
570575
def codes(self) -> np.ndarray:

pandas/tests/window/test_grouper.py

+15
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,18 @@ def test_groupby_rolling_string_index(self):
457457
columns=["index", "group", "eventTime", "count_to_date"],
458458
).set_index(["group", "index"])
459459
tm.assert_frame_equal(result, expected)
460+
461+
def test_groupby_rolling_no_sort(self):
462+
# GH 36889
463+
result = (
464+
pd.DataFrame({"foo": [2, 1], "bar": [2, 1]})
465+
.groupby("foo", sort=False)
466+
.rolling(1)
467+
.min()
468+
)
469+
expected = pd.DataFrame(
470+
np.array([[2.0, 2.0], [1.0, 1.0]]),
471+
columns=["foo", "bar"],
472+
index=pd.MultiIndex.from_tuples([(2, 0), (1, 1)], names=["foo", None]),
473+
)
474+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)