Skip to content

BUG: groupby with no non-empty groups, #21624 #21849

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

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def size(self):
if ngroup:
out = np.bincount(ids[ids != -1], minlength=ngroup)
else:
out = ids
out = []
Copy link
Member

Choose a reason for hiding this comment

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

Hmm something just seems strange to me about this. So your test example works fine, but what happens if we have a DataFrame that has other groups besides the all-NA one? Can you add a test for that and make sure that still works?

Copy link
Author

Choose a reason for hiding this comment

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

Done. In that case, the else block should never be used

return Series(out,
index=self.result_index,
dtype='int64')
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,16 @@ def test_any_all_np_func(func):

res = df.groupby('key')['val'].transform(func)
tm.assert_series_equal(res, exp)


@pytest.mark.parametrize("input_df, expected", [
(DataFrame({'groups': [np.nan, np.nan, np.nan], 'values': [1, 2, 3]}),
Series([np.nan, np.nan, np.nan], name='values')),
(DataFrame({'groups': [np.nan, 'A', 'A', 'B', 'B'], 'values': range(5)}),
Series([np.nan, 3, 3, 7, 7], name='values'))
])
def test_transform_with_all_nan(input_df, expected):
# GH 21624
grouped = input_df.groupby('groups')
result = grouped['values'].transform('sum')
tm.assert_series_equal(result, expected)