Skip to content

Commit de664e8

Browse files
authored
Removed Need for OHLC As First Element if Used in .agg (#21769)
1 parent 620abc4 commit de664e8

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
@@ -3549,13 +3549,11 @@ def _aggregate_multiple_funcs(self, arg, _level):
35493549
obj._selection = name
35503550
results[name] = obj.aggregate(func)
35513551

3552-
if isinstance(list(compat.itervalues(results))[0],
3553-
DataFrame):
3554-
3552+
if any(isinstance(x, DataFrame) for x in compat.itervalues(results)):
35553553
# let higher level handle
35563554
if _level:
35573555
return results
3558-
return list(compat.itervalues(results))[0]
3556+
35593557
return DataFrame(results, columns=columns)
35603558

35613559
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)