BUG fixes tuple agg  issue 18079
State: closed

- [x] closes #18079
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Bug
name: Groupby
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=h1) Report
> Merging [#18354](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/d1010643fea058ba43c2c7124af75cc462ccf242?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/18354/graphs/tree.svg?src=pr&token=eZ4WkYLtcO&width=650&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #18354   +/-   ##
=======================================
  Coverage   91.33%   91.33%           
=======================================
  Files         163      163           
  Lines       49801    49801           
=======================================
  Hits        45487    45487           
  Misses       4314     4314
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.13% <100%> (ø)` | :arrow_up: |
| #single | `40.79% <0%> (+0.02%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby.py](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS5weQ==) | `92.03% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=footer). Last update [d101064...7a4342f](https://codecov.io/gh/pandas-dev/pandas/pull/18354?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@TomAugspurger's  comment on whats new prompted me to add a test for `list` aggregation only to discover a new issue.   

This works
df.groupby('A')['C'].aggregate(list)

This doesn't.  
df.groupby(['A', 'B']).aggregate(list)

Should I submit a separate issue for this or investigate and fix this new error as part of this PR?

<details>
  <summary>Click to see full traceback</summary>
  ---------------------------------------------------------------------------

Traceback (most recent call last):
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 2267, in agg_series
    return self._aggregate_series_fast(obj, func)
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 2287, in _aggregate_series_fast
    result, counts = grouper.get_result()
  File "pandas/_libs/src/reduce.pyx", line 406, in pandas._libs.lib.SeriesGrouper.get_result
    raise
  File "pandas/_libs/src/reduce.pyx", line 394, in pandas._libs.lib.SeriesGrouper.get_result
    result = _get_result_array(res,
  File "pandas/_libs/src/reduce.pyx", line 15, in pandas._libs.lib._get_result_array
    raise ValueError('function does not reduce')
ValueError: function does not reduce

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 4192, in aggregate
    return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 3634, in aggregate
    return self._python_agg_general(arg, *args, **kwargs)
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 853, in _python_agg_general
    result, counts = self.grouper.agg_series(obj, f)
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 2269, in agg_series
    return self._aggregate_series_pure_python(obj, func)
  File "/Users/bob/dev/pandas-dev/pandas/core/groupby.py", line 2304, in _aggregate_series_pure_python
    raise ValueError('Function does not reduce')
ValueError: Function does not reduce

</details>

- Moved tests to test_aggregate.py

- Parametrize the tests (one for DataFrames and one for Series))

- I fixed the list issue I mentioned above by removing the list condition in the following if

https://github.com/pandas-dev/pandas/blob/4fce7846be56e12999fe8758abb2ea2f2794259d/pandas/core/groupby.py#L2303

I was a little leery doing this, but all tests passed.

Note: Both np.array and pd.Series still error out with both scenarios (grouping by one column vs multiple columns), but with different error descriptions.  That said, is it possible to proceed with this PR since both tuple and list now behave the same way with single and multiple column groupbys?
```
df = pd.DataFrame({'A' : [1, 1, 3], 'B' :  [1, 1, 4], 'C' :  [1, 3, 4]})

df.groupby('A')['C'].aggregate(pd.Series) # np.array
>> Exception: Must produce aggregated value

df.groupby(['A', 'B']).aggregate(pd.Series) # np.array
>> ValueError: Function does not reduce
```

This might close https://github.com/pandas-dev/pandas/issues/4293 as well
Thanks @jreback!
one more rebase :>
Hello @bobhaffner! Thanks for updating the PR.





Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on November 26, 2017 at 16:18 Hours UTC
Hi @jreback I had conflicts (whatsnew) with the rebase, but I believe Its corrected now.  That said, I'm puzzled by the pep8speaks comment regarding `test_agg.py` though as I didn't modify that file.  My apologies for the confusion. 
Yikes, my mistake.  thanks Jeff.  
should be good to go. ping on green.
Thanks Jeff!  You're a lifesaver!

thanks @bobhaffner 
Many thanks for the help and the patience, @jreback .   Same to you @TomAugspurger