Skip to content

Backport PR #21769 on branch 0.23.x #21787

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions doc/source/whatsnew/v0.23.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Fixed Regressions
Bug Fixes
~~~~~~~~~

**Groupby/Resample/Rolling**

- Bug where calling :func:`DataFrameGroupBy.agg` with a list of functions including ``ohlc`` as the non-initial element would raise a ``ValueError`` (:issue:`21716`)
-

**Conversion**

-
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3557,13 +3557,11 @@ def _aggregate_multiple_funcs(self, arg, _level):
obj._selection = name
results[name] = obj.aggregate(func)

if isinstance(list(compat.itervalues(results))[0],
DataFrame):

if any(isinstance(x, DataFrame) for x in compat.itervalues(results)):
# let higher level handle
if _level:
return results
return list(compat.itervalues(results))[0]

return DataFrame(results, columns=columns)

def _wrap_output(self, output, index, names=None):
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,3 +1674,22 @@ def test_tuple_correct_keyerror():
[3, 4]]))
with tm.assert_raises_regex(KeyError, "(7, 8)"):
df.groupby((7, 8)).mean()


def test_groupby_agg_ohlc_non_first():
# GH 21716
df = pd.DataFrame([[1], [1]], columns=['foo'],
index=pd.date_range('2018-01-01', periods=2, freq='D'))

expected = pd.DataFrame([
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
], columns=pd.MultiIndex.from_tuples((
('foo', 'ohlc', 'open'), ('foo', 'ohlc', 'high'),
('foo', 'ohlc', 'low'), ('foo', 'ohlc', 'close'),
('foo', 'sum', 'foo'))), index=pd.date_range(
'2018-01-01', periods=2, freq='D'))

result = df.groupby(pd.Grouper(freq='D')).agg(['sum', 'ohlc'])

tm.assert_frame_equal(result, expected)