Skip to content

Commit ee549ed

Browse files
committed
Fixed issue with observed=False and nth
1 parent 03ee26b commit ee549ed

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pandas/core/groupby/groupby.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class providing the base-class of operations.
4242
from pandas.core.frame import DataFrame
4343
from pandas.core.generic import NDFrame
4444
from pandas.core.groupby import base
45-
from pandas.core.index import Index, MultiIndex
45+
from pandas.core.index import CategoricalIndex, Index, MultiIndex
4646
from pandas.core.series import Series
4747
from pandas.core.sorting import get_group_index_sorter
4848

@@ -839,6 +839,7 @@ def _cython_transform(self, how, numeric_only=True, **kwargs):
839839
def _cython_agg_general(self, how, alt=None, numeric_only=True,
840840
min_count=-1):
841841
output = {}
842+
842843
for name, obj in self._iterate_slices():
843844
is_numeric = is_numeric_dtype(obj.dtype)
844845
if numeric_only and not is_numeric:
@@ -1707,7 +1708,12 @@ def nth(self,
17071708
if not self.as_index:
17081709
return out
17091710

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)
17111717

17121718
return out.sort_index() if self.sort else out
17131719

0 commit comments

Comments
 (0)