Skip to content
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

BUG: fixed issue with mixed type groupby aggregate #17003

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ Groupby/Resample/Rolling
- Bug in :func:`infer_freq` causing indices with 2-day gaps during the working week to be wrongly inferred as business daily (:issue:`16624`)
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 0.22.0

- Bug in ``Grouper.aggregate()`` where grouping by a vector of mixed types would raise a ``TypeError`` only if the aggregating function returned a list-like object (:issue:`16916`)
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)
- Bug in ``.rolling(...).apply(...)`` with a ``DataFrame`` with a ``DatetimeIndex``, a ``window`` of a timedelta-convertible and ``min_periods >= 1` (:issue:`15305`)
- Bug in ``DataFrame.groupby`` where index and column keys were not recognized correctly when the number of keys equaled the number of elements on the groupby axis (:issue:`16859`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from pandas.io.formats.printing import pprint_thing
from pandas.util._validators import validate_kwargs

from pandas.core.sorting import safe_sort
import pandas.core.algorithms as algorithms
import pandas.core.common as com
from pandas.core.config import option_context
Expand Down Expand Up @@ -2897,7 +2898,7 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
except Exception:
result = self._aggregate_named(func_or_funcs, *args, **kwargs)

index = Index(sorted(result), name=self.grouper.names[0])
index = Index(safe_sort(result), name=self.grouper.names[0])
ret = Series(result, index=index)

if not self.as_index: # pragma: no cover
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/groupby/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,17 @@ def test_sum_uint64_overflow(self):
expected.index.name = 0
result = df.groupby(0).sum()
tm.assert_frame_equal(result, expected)

def test_mixed_type_grouping(self):
Copy link
Member

@gfyoung gfyoung Jul 18, 2017

Choose a reason for hiding this comment

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

Refer the issue number beneath the function definition (e.g. "see gh-19616")

# see gh-19616
expected = pd.DataFrame(data=[[[1, 1], [2, 2], [3, 3]],
[[1, 1], [2, 2], [3, 3]]],
columns=['X', 'Y', 'Z'],
index=pd.Index(data=[2, 'g1'],
name='grouping'))

df = pd.DataFrame(data=[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]],
columns=list('XYZ'), index=list('qwer'))
df['grouping'] = ['g1', 'g1', 2, 2]
result = df.groupby('grouping').aggregate(lambda x: x.tolist())
tm.assert_frame_equal(result, expected)