Skip to content

Fix DataFrame.agg produces different types if the DataFrame is empty #41722

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 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8534,6 +8534,9 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
result_in_dict = relabel_result(result, func, columns, order)
result = DataFrame(result_in_dict, index=columns)

if isinstance(result, DataFrame):
result = result.squeeze()

return result

agg = aggregate
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/frame/test_aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np

Copy link
Contributor

Choose a reason for hiding this comment

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

these go in pandas/tests/apply/test_frame_apply.py

there are already extensive tests, so find a good place to exercise the empty case

we have these, so model after

(pandas) jreback@lhs2:~/pandas-dev$ grep -r empty pandas/tests/apply/|grep def
pandas/tests/apply/test_frame_transform.py:def test_transform_empty_listlike(float_frame, ops, frame_or_series):
pandas/tests/apply/test_frame_transform.py:def test_transform_empty_dictlike(float_frame, ops, frame_or_series):
pandas/tests/apply/test_frame_transform.py:def test_transform_empty_dataframe():
pandas/tests/apply/test_series_apply.py:def test_apply_empty_integer_series_with_datetime_index():
pandas/tests/apply/test_series_apply.py:def test_map_empty(index):
pandas/tests/apply/test_frame_apply.py:def test_apply_empty(float_frame):
pandas/tests/apply/test_frame_apply.py:def test_apply_with_reduce_empty():
pandas/tests/apply/test_frame_apply.py:def test_apply_funcs_over_empty(func):
pandas/tests/apply/test_frame_apply.py:def test_nunique_empty():
pandas/tests/apply/test_frame_apply.py:def test_apply_empty_infer_type():
pandas/tests/apply/test_frame_apply.py:def test_apply_empty_list_reduce():

import pandas as pd

from pandas import (
DataFrame,
Series,
)

import pandas._testing as tm


def _check_mixed_int(df, dtype=None):
# GH#41672
result = DataFrame([], columns=["lang", "name"])
result = result.agg({"name": lambda y: y.values})
assert type(result) == Series

result = DataFrame([["a", "boof"]], columns=["lang", "name"])
result = result.agg({"name": lambda y: y.values})
assert type(result) == Series