Skip to content

BUG/df.agg-with-df-with-missing-values-results-in-IndexError #58864

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
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ Datetimelike
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56382`)
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
- Bug in :meth:`Dataframe.agg` with df with missing values resulting in IndexError (:issue:`58810`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` does not raise on Custom business days frequencies bigger then "1C" (:issue:`58664`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` returning ``False`` on double-digit frequencies (:issue:`58523`)
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,11 +1825,13 @@ def relabel_result(
com.get_callable_name(f) if not isinstance(f, str) else f for f in fun
]
col_idx_order = Index(s.index).get_indexer(fun)
s = s.iloc[col_idx_order]

valid_idx = col_idx_order != -1
if valid_idx.any():
s = s.iloc[col_idx_order[valid_idx]]
# assign the new user-provided "named aggregation" as index names, and reindex
# it based on the whole user-provided names.
s.index = reordered_indexes[idx : idx + len(fun)]
if not s.empty:
s.index = reordered_indexes[idx : idx + len(fun)]
reordered_result_in_dict[col] = s.reindex(columns)
idx = idx + len(fun)
return reordered_result_in_dict
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ def test_agg_ser_multi_key(df):
tm.assert_series_equal(results, expected)


def test_agg_with_missing_values():
# GH#58810
missing_df = DataFrame(
{
"nan": [np.nan, np.nan, np.nan, np.nan],
"na": [pd.NA, pd.NA, pd.NA, pd.NA],
"nat": [pd.NaT, pd.NaT, pd.NaT, pd.NaT],
"none": [None, None, None, None],
"values": [1, 2, 3, 4],
}
)

result = missing_df.agg(x=("nan", "min"), y=("na", "min"), z=("values", "sum"))

expected = DataFrame(
{
"nan": [np.nan, np.nan, np.nan],
"na": [np.nan, np.nan, np.nan],
"values": [np.nan, np.nan, 10.0],
},
index=["x", "y", "z"],
)

tm.assert_frame_equal(result, expected)


def test_groupby_aggregation_mixed_dtype():
# GH 6212
expected = DataFrame(
Expand Down