Skip to content

Commit 120392d

Browse files
committed
1 parent bca35ff commit 120392d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/groupby/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def recode_for_groupby(
5353
unique_codes = unique1d(c.codes)
5454

5555
take_codes = unique_codes[unique_codes != -1]
56-
if c.ordered:
56+
if sort:
5757
take_codes = np.sort(take_codes)
5858

5959
# we recode according to the uniques

pandas/core/groupby/grouper.py

+11
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ def __init__(
502502
self._group_index,
503503
) = index._get_grouper_for_level(mapper, level=ilevel, dropna=dropna)
504504

505+
if is_categorical_dtype(self.grouping_vector):
506+
self._passed_categorical = True
507+
self._orig_cats = self.grouping_vector.categories
508+
509+
# Should the sort arg be just `sort` or `sort or self.grouping_vector.ordered`?
510+
self.grouping_vector, self._all_grouper = recode_for_groupby(
511+
self.grouping_vector, sort, observed
512+
)
513+
505514
# a passed Grouper like, directly get the grouper in the same way
506515
# as single grouper groupby, use the group_info to get codes
507516
elif isinstance(self.grouping_vector, Grouper):
@@ -529,6 +538,8 @@ def __init__(
529538
self._passed_categorical = True
530539

531540
self._orig_cats = self.grouping_vector.categories
541+
542+
# Should the sort arg be just `sort` or `sort or self.grouping_vector.ordered`?
532543
self.grouping_vector, self._all_grouper = recode_for_groupby(
533544
self.grouping_vector, sort, observed
534545
)

0 commit comments

Comments
 (0)