Skip to content

Commit de69d12

Browse files
committed
fix
1 parent 3b48b17 commit de69d12

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Other enhancements
3939
- Users can globally disable any ``PerformanceWarning`` by setting the option ``mode.performance_warnings`` to ``False`` (:issue:`56920`)
4040
- :meth:`Styler.format_index_names` can now be used to format the index and column names (:issue:`48936` and :issue:`47489`)
4141
- :class:`.errors.DtypeWarning` improved to include column names when mixed data types are detected (:issue:`58174`)
42+
- :meth:`DataFrame.agg` now correctly handles missing values without raising an IndexError (:issue:`58810`)
4243
- :meth:`DataFrame.corrwith` now accepts ``min_periods`` as optional arguments, as in :meth:`DataFrame.corr` and :meth:`Series.corr` (:issue:`9490`)
4344
- :meth:`DataFrame.cummin`, :meth:`DataFrame.cummax`, :meth:`DataFrame.cumprod` and :meth:`DataFrame.cumsum` methods now have a ``numeric_only`` parameter (:issue:`53072`)
4445
- :meth:`DataFrame.fillna` and :meth:`Series.fillna` can now accept ``value=None``; for non-object dtype the corresponding NA value will be used (:issue:`57723`)

pandas/core/apply.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1825,12 +1825,14 @@ def relabel_result(
18251825
com.get_callable_name(f) if not isinstance(f, str) else f for f in fun
18261826
]
18271827
col_idx_order = Index(s.index).get_indexer(fun)
1828+
col_idx_order = [idx for idx in col_idx_order if 0 <= idx < len(s)]
18281829
s = s.iloc[col_idx_order]
18291830

18301831
# assign the new user-provided "named aggregation" as index names, and reindex
18311832
# it based on the whole user-provided names.
1832-
s.index = reordered_indexes[idx : idx + len(fun)]
1833-
reordered_result_in_dict[col] = s.reindex(columns)
1833+
if len(s) > 0:
1834+
s.index = reordered_indexes[idx : idx + len(fun)]
1835+
reordered_result_in_dict[col] = s.reindex(columns)
18341836
idx = idx + len(fun)
18351837
return reordered_result_in_dict
18361838

pandas/tests/groupby/aggregate/test_aggregate.py

+18
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ def test_agg_ser_multi_key(df):
6262
tm.assert_series_equal(results, expected)
6363

6464

65+
def test_agg_with_missing_values():
66+
missing_df = DataFrame(
67+
{
68+
"nan": [np.nan, np.nan, np.nan, np.nan],
69+
"na": [pd.NA, pd.NA, pd.NA, pd.NA],
70+
"nat": [pd.NaT, pd.NaT, pd.NaT, pd.NaT],
71+
"none": [None, None, None, None],
72+
"values": [1, 2, 3, 4],
73+
}
74+
)
75+
76+
result = missing_df.agg(x=("nan", "min"), y=("na", "min"), z=("values", "sum"))
77+
78+
expected = DataFrame({"values": [np.nan, np.nan, 10]}, index=["x", "y", "z"])
79+
80+
tm.assert_frame_equal(result, expected)
81+
82+
6583
def test_groupby_aggregation_mixed_dtype():
6684
# GH 6212
6785
expected = DataFrame(

0 commit comments

Comments
 (0)