Skip to content

Commit ddc6442

Browse files
authored
Fix AttributeError when groupby as_index=False on empty DataFrame (#35324)
1 parent d33c801 commit ddc6442

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Diff for: doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ Groupby/resample/rolling
11301130
- Bug in :meth:`DataFrame.ewm.cov` was throwing ``AssertionError`` for :class:`MultiIndex` inputs (:issue:`34440`)
11311131
- Bug in :meth:`core.groupby.DataFrameGroupBy.quantile` raises ``TypeError`` for non-numeric types rather than dropping columns (:issue:`27892`)
11321132
- Bug in :meth:`core.groupby.DataFrameGroupBy.transform` when ``func='nunique'`` and columns are of type ``datetime64``, the result would also be of type ``datetime64`` instead of ``int64`` (:issue:`35109`)
1133+
- Bug in :meth:`DataFrame.groupby` raising an ``AttributeError`` when selecting a column and aggregating with ``as_index=False`` (:issue:`35246`).
11331134
- Bug in :meth:'DataFrameGroupBy.first' and :meth:'DataFrameGroupBy.last' that would raise an unnecessary ``ValueError`` when grouping on multiple ``Categoricals`` (:issue:`34951`)
11341135

11351136
Reshaping

Diff for: pandas/core/groupby/generic.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -963,18 +963,23 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
963963
# try to treat as if we are passing a list
964964
try:
965965
result = self._aggregate_multiple_funcs([func], _axis=self.axis)
966-
except ValueError as err:
967-
if "no results" not in str(err):
968-
# raised directly by _aggregate_multiple_funcs
969-
raise
970-
result = self._aggregate_frame(func)
971-
else:
966+
972967
# select everything except for the last level, which is the one
973968
# containing the name of the function(s), see GH 32040
974969
result.columns = result.columns.rename(
975970
[self._selected_obj.columns.name] * result.columns.nlevels
976971
).droplevel(-1)
977972

973+
except ValueError as err:
974+
if "no results" not in str(err):
975+
# raised directly by _aggregate_multiple_funcs
976+
raise
977+
result = self._aggregate_frame(func)
978+
except AttributeError:
979+
# catch exception from line 969
980+
# (Series does not have attribute "columns"), see GH 35246
981+
result = self._aggregate_frame(func)
982+
978983
if relabeling:
979984

980985
# used reordered index of columns

Diff for: pandas/tests/groupby/test_groupby.py

+8
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ def test_as_index_select_column():
605605
tm.assert_series_equal(result, expected)
606606

607607

608+
def test_groupby_as_index_select_column_sum_empty_df():
609+
# GH 35246
610+
df = DataFrame(columns=["A", "B", "C"])
611+
left = df.groupby(by="A", as_index=False)["B"].sum()
612+
assert type(left) is DataFrame
613+
assert left.to_dict() == {"A": {}, "B": {}}
614+
615+
608616
def test_groupby_as_index_agg(df):
609617
grouped = df.groupby("A", as_index=False)
610618

0 commit comments

Comments
 (0)