Skip to content
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

Fix 33634: If aggregation function returns NaN the order of the index on the resulting df is not maintained #41017

Merged
merged 16 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,11 @@ Other
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)
<<<<<<< HEAD
DriesSchaumont marked this conversation as resolved.
Show resolved Hide resolved
- Bug in :meth:`DataFrame.agg()` not sorting the non-concatenated axis in the order of the provided aggragation functions when one or more aggregation function fails to produce results (:issue:`33634`).
DriesSchaumont marked this conversation as resolved.
Show resolved Hide resolved
=======
- Bug in :meth:`DataFrame.clip` not interpreting missing values as no threshold (:issue:`40420`)
>>>>>>> upstream/master

.. ---------------------------------------------------------------------------

Expand Down
14 changes: 11 additions & 3 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,10 @@ def agg_list_like(self) -> FrameOrSeriesUnion:
raise ValueError("no results")

try:
return concat(results, keys=keys, axis=1, sort=False)
concatenated = concat(results, keys=keys, axis=1, sort=False)
except TypeError as err:

# we are concatting non-NDFrame objects,
# e.g. a list of scalars

from pandas import Series

result = Series(results, index=keys, name=obj.name)
Expand All @@ -396,6 +394,16 @@ def agg_list_like(self) -> FrameOrSeriesUnion:
"cannot combine transform and aggregation operations"
) from err
return result
else:
# Concat uses the first index to determine the final indexing order.
DriesSchaumont marked this conversation as resolved.
Show resolved Hide resolved
# The union of a shorter first index with the other indices causes
# the index sorting to be different from the order of the aggregating
# functions. Reindex if this is the case.
index_size = concatenated.index.size
good_index = next(
DriesSchaumont marked this conversation as resolved.
Show resolved Hide resolved
result.index for result in results if result.index.size == index_size
)
return concatenated.reindex(good_index, copy=False)

def agg_dict_like(self) -> FrameOrSeriesUnion:
"""
Expand Down
39 changes: 35 additions & 4 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,10 +1109,9 @@ def test_agg_multiple_mixed_no_warning():
with tm.assert_produces_warning(None):
result = mdf[["D", "C", "B", "A"]].agg(["sum", "min"])

# For backwards compatibility, the result's index is
# still sorted by function name, so it's ['min', 'sum']
# not ['sum', 'min'].
expected = expected[["D", "C", "B", "A"]]
# GH40420: the result of .agg should have an index that is sorted
# according to the arguments provided to agg.
expected = expected[["D", "C", "B", "A"]].reindex(["sum", "min"])
tm.assert_frame_equal(result, expected)


Expand Down Expand Up @@ -1518,3 +1517,35 @@ def test_apply_np_reducer(float_frame, op, how):
getattr(np, op)(float_frame, axis=0, **kwargs), index=float_frame.columns
)
tm.assert_series_equal(result, expected)


def test_aggregation_func_column_order():
# GH40420: the result of .agg should have an index that is sorted
# according to the arguments provided to agg.
df = DataFrame(
[
("1", 1, 0, 0),
("2", 2, 0, 0),
("3", 3, 0, 0),
("4", 4, 5, 4),
("5", 5, 6, 6),
("6", 6, 7, 7),
],
columns=("item", "att1", "att2", "att3"),
)

def foo(s):
return s.sum() / 2

aggs = ["sum", foo, "count", "min"]
result = df.agg(aggs)
expected = DataFrame(
{
"item": ["123456", np.nan, 6, "1"],
"att1": [21.0, 10.5, 6.0, 1.0],
"att2": [18.0, 9.0, 6.0, 0.0],
"att3": [17.0, 8.5, 6.0, 0.0],
},
index=["sum", "foo", "count", "min"],
)
tm.assert_frame_equal(result, expected)