Skip to content

Commit 7ad3684

Browse files
committed
fix
1 parent b162331 commit 7ad3684

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
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

+7-4
Original file line numberDiff line numberDiff line change
@@ -1824,13 +1824,16 @@ def relabel_result(
18241824
fun = [
18251825
com.get_callable_name(f) if not isinstance(f, str) else f for f in fun
18261826
]
1827-
col_idx_order = Index(s.index).get_indexer(fun)
1828-
s = s.iloc[col_idx_order]
1827+
col_idx_order = list(Index(s.index).get_indexer(fun))
1828+
col_idx_order = [i for i in col_idx_order if 0 <= i < len(s)]
1829+
if col_idx_order:
1830+
s = s.iloc[col_idx_order]
18291831

18301832
# assign the new user-provided "named aggregation" as index names, and reindex
18311833
# 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)
1834+
if len(s) > 0:
1835+
s.index = reordered_indexes[idx : idx + len(fun)]
1836+
reordered_result_in_dict[col] = s.reindex(columns)
18341837
idx = idx + len(fun)
18351838
return reordered_result_in_dict
18361839

pandas/tests/groupby/aggregate/test_aggregate.py

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

6464

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

0 commit comments

Comments
 (0)