Skip to content

Commit 816aa17

Browse files
committed
Included NammedAgg changes in whatsnew
1 parent 8ba87ac commit 816aa17

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Other enhancements
3131
- :class:`pandas.api.typing.FrozenList` is available for typing the outputs of :attr:`MultiIndex.names`, :attr:`MultiIndex.codes` and :attr:`MultiIndex.levels` (:issue:`58237`)
3232
- :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`)
3333
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
34+
- :func:`pandas.NamedAgg` now forwards any *args and **kwargs to calls of ``aggfunc`` (:issue:`58283`)
3435
- :func:`read_stata` now returns ``datetime64`` resolutions better matching those natively stored in the stata format (:issue:`55642`)
3536
- :meth:`Styler.set_tooltips` provides alternative method to storing tooltips by using title attribute of td elements. (:issue:`56981`)
3637
- Allow dictionaries to be passed to :meth:`pandas.Series.str.replace` via ``pat`` parameter (:issue:`51748`)
@@ -43,7 +44,7 @@ Other enhancements
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`)
4546
- :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`)
46-
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`)
47+
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie chgit aarts, allowing for explicit control over the y-axis label (:issue:`58239`)
4748
- Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)
4849
-
4950

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,34 @@ def test_agg_namedtuple(self):
827827
expected = df.groupby("A").agg(b=("B", "sum"), c=("B", "count"))
828828
tm.assert_frame_equal(result, expected)
829829

830+
def test_single_named_agg_with_args_and_kwargs(self):
831+
df = DataFrame({"A": [0, 1, 2, 3], "B": [1, 2, 3, 4]})
832+
833+
def n_between(ser, low, high):
834+
return ser.between(low, high).sum()
835+
836+
result = df.groupby("A").agg(n_between=pd.NamedAgg("B", n_between, 0, high=2))
837+
expected = df.groupby("A").agg(n_between=("B", lambda x: x.between(0, 2).sum()))
838+
tm.assert_frame_equal(result, expected)
839+
840+
def test_multiple_named_agg_with_args_and_kwargs(self):
841+
df = DataFrame({"A": [0, 1, 2, 3], "B": [1, 2, 3, 4]})
842+
843+
def n_between(ser, low, high):
844+
return ser.between(low, high).sum()
845+
846+
result = df.groupby("A").agg(
847+
n_between01=pd.NamedAgg("B", n_between, 0, 1),
848+
n_between13=pd.NamedAgg("B", n_between, 1, 3),
849+
n_between02=pd.NamedAgg("B", n_between, 0, 2),
850+
)
851+
expected = df.groupby("A").agg(
852+
n_between01=("B", lambda x: x.between(0, 1).sum()),
853+
n_between13=("B", lambda x: x.between(0, 3).sum()),
854+
n_between02=("B", lambda x: x.between(0, 2).sum()),
855+
)
856+
tm.assert_frame_equal(result, expected)
857+
830858
def test_mangled(self):
831859
df = DataFrame({"A": [0, 1], "B": [1, 2], "C": [3, 4]})
832860
result = df.groupby("A").agg(b=("B", lambda x: 0), c=("C", lambda x: 1))

0 commit comments

Comments
 (0)