Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix groupby().count() for datetime columns #18167

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Bug Fixes
- Bug in :class:`TimedeltaIndex` subtraction could incorrectly overflow when ``NaT`` is present (:issue:`17791`)
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
- Bug in ``pd.DataFrameGroupBy.count()`` when counting over a datetimelike column (:issue:`13393`)

Conversion
^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4365,7 +4365,8 @@ def count(self):
ids, _, ngroups = self.grouper.group_info
mask = ids != -1

val = ((mask & ~isna(blk.get_values())) for blk in data.blocks)
val = ((mask & ~isna(np.atleast_2d(blk.get_values())))
for blk in data.blocks)
loc = (blk.mgr_locs for blk in data.blocks)

counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)
Expand Down
21 changes: 19 additions & 2 deletions pandas/tests/groupby/test_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from __future__ import print_function

import numpy as np
import pytest

from pandas import (DataFrame, Series, MultiIndex)
from pandas.util.testing import assert_series_equal
from pandas import (DataFrame, Series, MultiIndex, Timestamp, Timedelta,
Period)
from pandas.util.testing import (assert_series_equal, assert_frame_equal)
from pandas.compat import (range, product as cart_product)


Expand Down Expand Up @@ -195,3 +197,18 @@ def test_ngroup_respects_groupby_order(self):
g.ngroup())
assert_series_equal(Series(df['group_index'].values),
g.cumcount())

@pytest.mark.parametrize('datetimelike', [
[Timestamp('2016-05-%02d 20:09:25+00:00' % i) for i in range(1, 4)],
[Timestamp('2016-05-%02d 20:09:25' % i) for i in range(1, 4)],
[Timedelta(x, unit="h") for x in range(1, 4)],
[Period(freq="2W", year=2017, month=x) for x in range(1, 4)]])
def test_count_with_datetimelike(self, datetimelike):
# test for #13393, where DataframeGroupBy.count() fails
# when counting a datetimelike column.

df = DataFrame({'x': ['a', 'a', 'b'], 'y': datetimelike})
res = df.groupby('x').count()
expected = DataFrame({'y': [2, 1]}, index=['a', 'b'])
expected.index.name = "x"
assert_frame_equal(expected, res)