Skip to content

Removed Need for OHLC As First Element if Used in .agg #21769

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

Merged
merged 5 commits into from
Jul 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3549,13 +3549,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]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no test coverage for this line and I can't feasibly think how this would be useful, hence the deletion


return DataFrame(results, columns=columns)

def _wrap_output(self, output, index, names=None):
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,3 +1674,21 @@ 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():
df = pd.DataFrame([[1], [1]], columns=['foo'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number as a comment

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)