Skip to content

Commit 45f078a

Browse files
committed
BUG fixes tuple agg 18079
1 parent cfad581 commit 45f078a

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Bug Fixes
6262
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
6363
- Bug in ``pd.DataFrameGroupBy.count()`` when counting over a datetimelike column (:issue:`13393`)
6464
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
65+
- Bug in :class:`NDFrameGroupBy` fixes ValueError: no results error when grouping by a single column and aggregating with a tuple (:issue:`18079`)
6566

6667
Conversion
6768
^^^^^^^^^^

pandas/core/groupby.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3022,7 +3022,9 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
30223022
if isinstance(func_or_funcs, compat.string_types):
30233023
return getattr(self, func_or_funcs)(*args, **kwargs)
30243024

3025-
if hasattr(func_or_funcs, '__iter__'):
3025+
if isinstance(func_or_funcs, collections.Iterable):
3026+
# Catch instances of lists / tuples
3027+
# but not the class list / tuple itself.
30263028
ret = self._aggregate_multiple_funcs(func_or_funcs,
30273029
(_level or 0) + 1)
30283030
else:

pandas/tests/groupby/test_groupby.py

+16
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,22 @@ def test_empty_dataframe_groupby(self):
27182718

27192719
assert_frame_equal(result, expected)
27202720

2721+
def test_tuple_aggregation(self):
2722+
# Issue #18079
2723+
df = pd.DataFrame({'A': [1, 1, 1, 3, 3, 3],
2724+
'B': [1, 1, 1, 4, 4, 4], 'C': [1, 1, 1, 3, 4, 4]})
2725+
2726+
result = df.groupby(['A', 'B']).aggregate(tuple)
2727+
expected = pd.DataFrame({'C': {(1, 1): (1, 1, 1), (3, 4): (3, 4, 4)}})
2728+
expected.index.names = ['A', 'B']
2729+
assert_frame_equal(result, expected)
2730+
2731+
result = df.groupby('A')['C'].aggregate(tuple)
2732+
expected = pd.Series([(1, 1, 1), (3, 4, 4)], index=[1, 3])
2733+
expected.name = 'C'
2734+
expected.index.name = 'A'
2735+
assert_series_equal(result, expected)
2736+
27212737

27222738
def _check_groupby(df, result, keys, field, f=lambda x: x.sum()):
27232739
tups = lmap(tuple, df[keys].values)

0 commit comments

Comments
 (0)