Skip to content

Commit 0fe3430

Browse files
WillAydMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #21769: Removed Need for OHLC As First Element if Used in .agg
1 parent e2f65df commit 0fe3430

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

doc/source/whatsnew/v0.23.3.txt

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Fixed Regressions
2424
Bug Fixes
2525
~~~~~~~~~
2626

27+
**Groupby/Resample/Rolling**
28+
29+
- Bug where calling :func:`DataFrameGroupBy.agg` with a list of functions including ``ohlc`` as the non-initial element would raise a ``ValueError`` (:issue:`21716`)
30+
-
31+
2732
**Conversion**
2833

2934
-

pandas/core/groupby/groupby.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3557,13 +3557,11 @@ def _aggregate_multiple_funcs(self, arg, _level):
35573557
obj._selection = name
35583558
results[name] = obj.aggregate(func)
35593559

3560-
if isinstance(list(compat.itervalues(results))[0],
3561-
DataFrame):
3562-
3560+
if any(isinstance(x, DataFrame) for x in compat.itervalues(results)):
35633561
# let higher level handle
35643562
if _level:
35653563
return results
3566-
return list(compat.itervalues(results))[0]
3564+
35673565
return DataFrame(results, columns=columns)
35683566

35693567
def _wrap_output(self, output, index, names=None):

pandas/tests/groupby/test_groupby.py

+19
Original file line numberDiff line numberDiff line change
@@ -1674,3 +1674,22 @@ def test_tuple_correct_keyerror():
16741674
[3, 4]]))
16751675
with tm.assert_raises_regex(KeyError, "(7, 8)"):
16761676
df.groupby((7, 8)).mean()
1677+
1678+
1679+
def test_groupby_agg_ohlc_non_first():
1680+
# GH 21716
1681+
df = pd.DataFrame([[1], [1]], columns=['foo'],
1682+
index=pd.date_range('2018-01-01', periods=2, freq='D'))
1683+
1684+
expected = pd.DataFrame([
1685+
[1, 1, 1, 1, 1],
1686+
[1, 1, 1, 1, 1]
1687+
], columns=pd.MultiIndex.from_tuples((
1688+
('foo', 'ohlc', 'open'), ('foo', 'ohlc', 'high'),
1689+
('foo', 'ohlc', 'low'), ('foo', 'ohlc', 'close'),
1690+
('foo', 'sum', 'foo'))), index=pd.date_range(
1691+
'2018-01-01', periods=2, freq='D'))
1692+
1693+
result = df.groupby(pd.Grouper(freq='D')).agg(['sum', 'ohlc'])
1694+
1695+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)