@@ -42,7 +42,7 @@ class providing the base-class of operations.
42
42
from pandas .core .frame import DataFrame
43
43
from pandas .core .generic import NDFrame
44
44
from pandas .core .groupby import base
45
- from pandas .core .index import Index , MultiIndex
45
+ from pandas .core .index import CategoricalIndex , Index , MultiIndex
46
46
from pandas .core .series import Series
47
47
from pandas .core .sorting import get_group_index_sorter
48
48
@@ -839,6 +839,7 @@ def _cython_transform(self, how, numeric_only=True, **kwargs):
839
839
def _cython_agg_general (self , how , alt = None , numeric_only = True ,
840
840
min_count = - 1 ):
841
841
output = {}
842
+
842
843
for name , obj in self ._iterate_slices ():
843
844
is_numeric = is_numeric_dtype (obj .dtype )
844
845
if numeric_only and not is_numeric :
@@ -1707,7 +1708,12 @@ def nth(self,
1707
1708
if not self .as_index :
1708
1709
return out
1709
1710
1710
- out .index = self .grouper .result_index [ids [mask ]]
1711
+ result_index = self .grouper .result_index
1712
+ out .index = result_index [ids [mask ]]
1713
+
1714
+ if not self .observed and isinstance (
1715
+ result_index , CategoricalIndex ):
1716
+ out = out .reindex (result_index )
1711
1717
1712
1718
return out .sort_index () if self .sort else out
1713
1719
0 commit comments