Skip to content

BUG: groupby.apply raising a TypeError when __getitem__ selects multlple columns #53207

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 4 commits into from
May 15, 2023
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ Groupby/resample/rolling
or :class:`PeriodIndex`, and the ``groupby`` method was given a function as its first argument,
the function operated on the whole index rather than each element of the index. (:issue:`51979`)
- Bug in :meth:`DataFrameGroupBy.apply` causing an error to be raised when the input :class:`DataFrame` was subset as a :class:`DataFrame` after groupby (``[['a']]`` and not ``['a']``) and the given callable returned :class:`Series` that were not all indexed the same. (:issue:`52444`)
- Bug in :meth:`DataFrameGroupBy.apply` raising a ``TypeError`` when selecting multiple columns and providing a function that returns ``np.ndarray`` results (:issue:`18930`)
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
- Bug in :meth:`GroupBy.quantile` may implicitly sort the result index with ``sort=False`` (:issue:`53009`)
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
-

Reshaping
^^^^^^^^^
Expand Down
14 changes: 11 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
CategoricalDtype,
IntervalDtype,
)
from pandas.core.dtypes.inference import is_hashable
from pandas.core.dtypes.missing import (
isna,
notna,
Expand Down Expand Up @@ -1474,9 +1475,16 @@ def _wrap_applied_output(
# fall through to the outer else clause
# TODO: sure this is right? we used to do this
# after raising AttributeError above
return self.obj._constructor_sliced(
values, index=key_index, name=self._selection
)
# GH 18930
if not is_hashable(self._selection):
# error: Need type annotation for "name"
name = tuple(self._selection) # type: ignore[var-annotated, arg-type]
else:
# error: Incompatible types in assignment
# (expression has type "Hashable", variable
# has type "Tuple[Any, ...]")
name = self._selection # type: ignore[assignment]
return self.obj._constructor_sliced(values, index=key_index, name=name)
elif not isinstance(first_not_none, Series):
# values are not series or array-like but scalars
# self._selection not passed through to Series as the
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,15 @@ def test_apply_inconsistent_output(group_col):
)

tm.assert_series_equal(result, expected)


def test_apply_array_output_multi_getitem():
# GH 18930
df = DataFrame(
{"A": {"a": 1, "b": 2}, "B": {"a": 1, "b": 2}, "C": {"a": 1, "b": 2}}
)
result = df.groupby("A")[["B", "C"]].apply(lambda x: np.array([0]))
expected = Series(
[np.array([0])] * 2, index=Index([1, 2], name="A"), name=("B", "C")
)
tm.assert_series_equal(result, expected)