Skip to content

Groupby agg works with pd.Series.nunique, but groupby nunique fails with axis=1 #30311

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 11 commits into from
Dec 18, 2019
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrame.groupby` where ``any``, ``all``, ``nunique`` and transform functions would incorrectly handle duplicate column labels (:issue:`21668`)
- Bug in :meth:`DataFrameGroupBy.agg` with timezone-aware datetime64 column incorrectly casting results to the original dtype (:issue:`29641`)
- Bug in :meth:`DataFrame.groupby` when using axis=1 and having a single level columns index (:issue:`30208`)
- Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`)

Reshaping
^^^^^^^^^
Expand Down
15 changes: 13 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1813,9 +1813,20 @@ def groupby_series(obj, col=None):
# Try to consolidate with normal wrapping functions
from pandas.core.reshape.concat import concat

results = [groupby_series(content, label) for label, content in obj.items()]
axis_number = obj._get_axis_number(self.axis)
Copy link
Contributor

Choose a reason for hiding this comment

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

@WillAyd this is adding quite a bit of very hard and bespoke code

pls don’t merge non trivial things like this

this is also likely non performant

pls revert

other_axis = int(not axis_number)
if axis_number == 0:
iter_func = obj.items
else:
iter_func = obj.iterrows

results = [groupby_series(content, label) for label, content in iter_func()]
results = concat(results, axis=1)
results.columns.names = obj.columns.names

if axis_number == 1:
results = results.T

results._get_axis(other_axis).names = obj._get_axis(other_axis).names

if not self.as_index:
results.index = ibase.default_index(len(results))
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1994,3 +1994,20 @@ def test_dup_labels_output_shape(groupby_func, idx):

assert result.shape == (1, 2)
tm.assert_index_equal(result.columns, idx)


def test_groupby_crash_on_nunique(axis):
# Fix following 30253
df = pd.DataFrame({("A", "B"): [1, 2], ("A", "C"): [1, 3], ("D", "B"): [0, 0]})

axis_number = df._get_axis_number(axis)
if not axis_number:
df = df.T

result = df.groupby(axis=axis_number, level=0).nunique()

expected = pd.DataFrame({"A": [1, 2], "D": [1, 1]})
if not axis_number:
expected = expected.T

tm.assert_frame_equal(result, expected)