Skip to content

REF: avoid passing empty list to concat in groupby methods #41231

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 1 commit into from
Apr 30, 2021
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
46 changes: 27 additions & 19 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,11 +1754,16 @@ def _iterate_column_groupbys(self):
def _apply_to_column_groupbys(self, func) -> DataFrame:
from pandas.core.reshape.concat import concat

return concat(
(func(col_groupby) for _, col_groupby in self._iterate_column_groupbys()),
keys=self._selected_obj.columns,
axis=1,
)
columns = self._selected_obj.columns
results = [
func(col_groupby) for _, col_groupby in self._iterate_column_groupbys()
]

if not len(results):
# concat would raise
return DataFrame([], columns=columns, index=self.grouper.result_index)
else:
return concat(results, keys=columns, axis=1)

def count(self) -> DataFrame:
"""
Expand Down Expand Up @@ -1850,27 +1855,30 @@ def nunique(self, dropna: bool = True) -> DataFrame:
# Try to consolidate with normal wrapping functions

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

results = concat(
[
SeriesGroupBy(content, selection=label, grouper=self.grouper).nunique(
dropna
)
for label, content in iter_func()
],
axis=1,
)
results = cast(DataFrame, results)
res_list = [
SeriesGroupBy(content, selection=label, grouper=self.grouper).nunique(
dropna
)
for label, content in iter_func()
]
if res_list:
results = concat(res_list, axis=1)
results = cast(DataFrame, results)
else:
# concat would raise
results = DataFrame(
[], index=self.grouper.result_index, columns=obj.columns[:0]
)

if axis_number == 1:
if self.axis == 1:
results = results.T

other_axis = 1 - self.axis
results._get_axis(other_axis).names = obj._get_axis(other_axis).names

if not self.as_index:
Expand Down
5 changes: 0 additions & 5 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,6 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
elif "len(index) != len(labels)" in str(err):
# raised in libgroupby validation
pass
elif "No objects to concatenate" in str(err):
# raised in concat call
# In tests this is reached via either
# _apply_to_column_groupbys (ohlc) or DataFrameGroupBy.nunique
pass
else:
raise

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,12 @@ def test_groupby_crash_on_nunique(axis):

tm.assert_frame_equal(result, expected)

# same thing, but empty columns
gb = df[[]].groupby(axis=axis_number, level=0)
res = gb.nunique()
exp = expected[[]]
tm.assert_frame_equal(res, exp)


def test_groupby_list_level():
# GH 9790
Expand Down