Skip to content

Commit 0daab80

Browse files
author
Diego Fernandez
committed
Ensure the right values are set in SeriesGroupBy.nunique
We only need to use the group boundaries as the index for `res` so that the dimensions match those of `out`. Fixes pandas-dev#13453
1 parent d6f8b46 commit 0daab80

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3032,7 +3032,7 @@ def nunique(self, dropna=True):
30323032
# we might have duplications among the bins
30333033
if len(res) != len(ri):
30343034
res, out = np.zeros(len(ri), dtype=out.dtype), res
3035-
res[ids] = out
3035+
res[ids[idx]] = out
30363036

30373037
return Series(res,
30383038
index=ri,

pandas/tests/groupby/test_groupby.py

+13
Original file line numberDiff line numberDiff line change
@@ -4159,6 +4159,19 @@ def test_nunique_with_empty_series(self):
41594159
expected = pd.Series(name='name', dtype='int64')
41604160
tm.assert_series_equal(result, expected)
41614161

4162+
def test_nunique_with_timegrouper(self):
4163+
# GH 13453
4164+
test = pd.DataFrame({
4165+
'time': [Timestamp('2016-06-28 09:35:35'),
4166+
Timestamp('2016-06-28 16:09:30'),
4167+
Timestamp('2016-06-28 16:46:28')],
4168+
'data': ['1', '2', '3']}).set_index('time')
4169+
result = test.groupby(pd.TimeGrouper(freq='h'))['data'].nunique()
4170+
expected = test.groupby(
4171+
pd.TimeGrouper(freq='h')
4172+
)['data'].apply(pd.Series.nunique)
4173+
tm.assert_series_equal(result, expected)
4174+
41624175
def test_numpy_compat(self):
41634176
# see gh-12811
41644177
df = pd.DataFrame({'A': [1, 2, 1], 'B': [1, 2, 3]})

0 commit comments

Comments
 (0)