Skip to content

Commit 1ed0f48

Browse files
committed
Fix groupby().count() for datetime columns
1 parent 93c755e commit 1ed0f48

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pandas/core/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4365,7 +4365,10 @@ def count(self):
43654365
ids, _, ngroups = self.grouper.group_info
43664366
mask = ids != -1
43674367

4368-
val = ((mask & ~isna(blk.get_values())) for blk in data.blocks)
4368+
makeNDarray = lambda vals: vals[None, :] if vals.ndim == 1 else vals
4369+
4370+
val = ((mask & ~isna(makeNDarray(blk.get_values())))
4371+
for blk in data.blocks)
43694372
loc = (blk.mgr_locs for blk in data.blocks)
43704373

43714374
counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)

pandas/tests/groupby/test_counting.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import numpy as np
55

6-
from pandas import (DataFrame, Series, MultiIndex)
7-
from pandas.util.testing import assert_series_equal
6+
from pandas import (DataFrame, Series, MultiIndex, Timestamp)
7+
from pandas.util.testing import (assert_series_equal, assert_frame_equal)
88
from pandas.compat import (range, product as cart_product)
99

1010

@@ -195,3 +195,13 @@ def test_ngroup_respects_groupby_order(self):
195195
g.ngroup())
196196
assert_series_equal(Series(df['group_index'].values),
197197
g.cumcount())
198+
199+
def test_count_with_datetime(self):
200+
df = DataFrame({'x': ['a', 'a', 'b'],
201+
'y': [Timestamp('2016-05-07 20:09:25+00:00'),
202+
Timestamp('2016-05-07 20:09:29+00:00'),
203+
Timestamp('2016-05-07 20:09:29+00:00')]})
204+
expected = DataFrame({'y': [2, 1]}, index=['a', 'b'])
205+
expected.index.name = "x"
206+
res = df.groupby('x').count()
207+
assert_frame_equal(expected, res)

0 commit comments

Comments
 (0)