Skip to content

Commit a13899e

Browse files
author
tascillo
committed
BUG: GroupBy.ngroup dropna=False inconsistency when using Categorical pandas-dev#50100
Added explicit nan check for test case to make it more readable on failure and implemented the fix.
1 parent b4a7947 commit a13899e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

pandas/core/groupby/groupby.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,8 +3256,12 @@ def ngroup(self, ascending: bool = True):
32563256

32573257
dtype: type
32583258
if self.grouper.has_dropped_na:
3259-
comp_ids = np.where(comp_ids == -1, np.nan, comp_ids)
3260-
dtype = np.float64
3259+
if self.grouper.dropna:
3260+
comp_ids = np.where(comp_ids == -1, np.nan, comp_ids)
3261+
dtype = np.float64
3262+
else:
3263+
comp_ids = np.where(comp_ids == -1, len(comp_ids) - 1, comp_ids)
3264+
dtype = np.int64
32613265
else:
32623266
dtype = np.int64
32633267

pandas/tests/groupby/test_missing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import math
23
import pytest
34

45
import pandas as pd
@@ -163,5 +164,5 @@ def test_categorical_nan_no_dropna():
163164
ng = g.ngroup()
164165
result = ng.iloc[0]
165166
expected = 0
166-
assert isinstance(result, int)
167+
assert not math.isnan(result)
167168
assert result == expected

0 commit comments

Comments
 (0)