Skip to content

Commit a7cd756

Browse files
committed
fixup! BUG: Fix .groupby(categorical, sort=False) failing
1 parent c813146 commit a7cd756

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

pandas/core/categorical.py

+15
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,21 @@ def _get_categories(self):
602602
categories = property(fget=_get_categories, fset=_set_categories,
603603
doc=_categories_doc)
604604

605+
def _codes_for_groupby(self, sort=False):
606+
""" Return a Categorical adjusted for groupby """
607+
cat = self
608+
# sort=False should order groups in as-encountered order (GH-8868)
609+
if not sort:
610+
cat = self.unique()
611+
all_categories = self.categories
612+
# But all categories should be present, including those missing
613+
# from the data (GH-13179), which .unique() dropped
614+
cat.add_categories(all_categories[
615+
~all_categories.isin(cat.categories)],
616+
inplace=True)
617+
cat = self.reorder_categories(cat.categories)
618+
return cat
619+
605620
_ordered = None
606621

607622
def set_ordered(self, value, inplace=False):

pandas/core/groupby.py

+1-22
Original file line numberDiff line numberDiff line change
@@ -2300,28 +2300,7 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
23002300
# a passed Categorical
23012301
elif is_categorical_dtype(self.grouper):
23022302

2303-
# must have an ordered categorical
2304-
if self.sort:
2305-
if not self.grouper.ordered:
2306-
2307-
# technically we cannot group on an unordered
2308-
# Categorical
2309-
# but this a user convenience to do so; the ordering
2310-
# is preserved and if it's a reduction it doesn't make
2311-
# any difference
2312-
pass
2313-
2314-
# fix bug #GH8868 sort=False being ignored in categorical
2315-
# groupby
2316-
else:
2317-
cat = self.grouper.unique()
2318-
all_categories = self.grouper.categories
2319-
cat.add_categories(
2320-
all_categories[
2321-
~all_categories.isin(cat.categories)],
2322-
inplace=True) # GH-13179
2323-
self.grouper = self.grouper.reorder_categories(
2324-
cat.categories)
2303+
self.grouper = self.grouper._codes_for_groupby(self.sort)
23252304

23262305
# we make a CategoricalIndex out of the cat grouper
23272306
# preserving the categories / ordered attributes

pandas/indexes/category.py

+4
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ def _append_same_dtype(self, to_concat, name):
550550
result.name = name
551551
return result
552552

553+
def _codes_for_groupby(self, sort):
554+
""" Return a Categorical adjusted for groupby """
555+
return self.values._codes_for_groupby(sort)
556+
553557
@classmethod
554558
def _add_comparison_methods(cls):
555559
""" add in comparison methods """

0 commit comments

Comments
 (0)