Skip to content

BUG: bug in GroupBy.count where arg minlength passed to np.bincount must be None for np<1.13 #21957

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

Merged
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
6 changes: 3 additions & 3 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,11 @@ Groupby/Resample/Rolling

- Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` with ``as_index=False`` leading to the loss of timezone information (:issue:`15884`)
- Bug in :meth:`DatetimeIndex.resample` when downsampling across a DST boundary (:issue:`8531`)
-
-

- Bug where ``ValueError`` is wrongly raised when calling :func:`~pandas.core.groupby.SeriesGroupBy.count` method of a
``SeriesGroupBy`` when the grouping variable only contains NaNs and numpy version < 1.13 (:issue:`21956`).
- Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'` and a
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
-

Sparse
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ def count(self):

mask = (ids != -1) & ~isna(val)
ids = ensure_platform_int(ids)
out = np.bincount(ids[mask], minlength=ngroups or 0)
out = np.bincount(ids[mask], minlength=ngroups or None)

return Series(out,
index=self.grouper.result_index,
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,13 @@ def test_count_with_datetimelike(self, datetimelike):
expected = DataFrame({'y': [2, 1]}, index=['a', 'b'])
expected.index.name = "x"
assert_frame_equal(expected, res)

def test_count_with_only_nans_in_first_group(self):
# GH21956
df = DataFrame({'A': [np.nan, np.nan], 'B': ['a', 'b'], 'C': [1, 2]})
result = df.groupby(['A', 'B']).C.count()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u do an assert_produces_warning()

here (should not show a warning)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not produce any warnings for me.

What should look for in travis? Grep "SeriesGroupBy.count"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just look at the warnings in the 3.6 log

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will look in the morning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, can you assert that this produces no warning (e.g. it IS producing a warning now in numpy < 1.13)

mi = MultiIndex(levels=[[], ['a', 'b']],
labels=[[], []],
names=['A', 'B'])
expected = Series([], index=mi, dtype=np.int64, name='C')
assert_series_equal(result, expected, check_index_type=False)