Skip to content

Commit d3bc362

Browse files
committed
update docs
add alt api for series agg with dict
1 parent a3deeaf commit d3bc362

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

doc/source/basics.rst

+12-4
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ Aggregation API
840840

841841
.. versionadded:: 0.20.0
842842

843-
The aggregation APi allows one to express possibly multiple aggregation operations in a single concise way.
843+
The aggregation API allows one to express possibly multiple aggregation operations in a single concise way.
844844
This API is similar across pandas objects, :ref:`groupby aggregates <groupby.aggregate>`,
845845
:ref:`window functions <stats.aggregate>`, and the :ref:`resample API <timeseries.aggregate>`.
846846

@@ -862,6 +862,9 @@ This will return a Series of the output.
862862
863863
tsdf.agg('sum')
864864
865+
# these are equivalent to a ``.sum()`` because we are aggregating on a single function
866+
tsdf.sum()
867+
865868
On a Series this will result in a scalar value
866869

867870
.. ipython:: python
@@ -915,6 +918,12 @@ For a Series, you can pass a dict; the keys will set the name of the column
915918
916919
tsdf.A.agg({'foo' : ['sum', 'mean']})
917920
921+
Alternatively, using multiple dictionaries, you can have renamed elements with the aggregation
922+
923+
.. ipython:: python
924+
925+
tsdf.A.agg({'foo' : 'sum', 'bar':'mean'})
926+
918927
Multiple keys will yield multiple columns.
919928

920929
.. ipython:: python
@@ -969,12 +978,11 @@ function name and user defined function.
969978
tsdf.transform('abs')
970979
tsdf.transform(lambda x: x.abs())
971980
972-
``.transform()`` with a single function is equivalent to applying a function across the
973-
columns.
981+
Since this is a single function, this is equivalent to a ufunc application
974982

975983
.. ipython:: python
976984
977-
tsdf.apply(np.abs, axis=1)
985+
np.abs(tsdf)
978986
979987
Passing a single function to ``.transform()`` with a Series will yield a single Series in return.
980988

pandas/core/generic.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -2782,17 +2782,16 @@ def pipe(self, func, *args, **kwargs):
27822782
27832783
Parameters
27842784
----------
2785-
arg : function or dict
2785+
func : callable, string, dictionary, or list of string/callables
27862786
Function to use for aggregating the data. If a function, must either
27872787
work when passed a DataFrame or when passed to DataFrame.apply. If
27882788
passed a dict, the keys must be DataFrame column names.
27892789
27902790
Accepted Combinations are:
2791-
- string cythonized function name
2792-
- function
2793-
- list of functions
2794-
- dict of columns -> functions
2795-
- nested dict of names -> dicts of functions
2791+
- string function name
2792+
- function
2793+
- list of functions
2794+
- dict of column names -> functions (or list of functions)
27962795
27972796
Notes
27982797
-----
@@ -2817,9 +2816,15 @@ def pipe(self, func, *args, **kwargs):
28172816
28182817
Parameters
28192818
----------
2820-
func : function, callable or string
2819+
func : callable, string, dictionary, or list of string/callables
28212820
To apply to column
28222821
2822+
Accepted Combinations are:
2823+
- string function name
2824+
- function
2825+
- list of functions
2826+
- dict of column names -> functions (or list of functions)
2827+
28232828
Examples
28242829
--------
28252830
>>> df.transform(lambda x: (x - x.mean()) / x.std())

pandas/tests/frame/test_apply.py

+3
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ def test_agg_transform(self):
482482
result = self.frame.apply(np.sqrt)
483483
assert_frame_equal(result, expected)
484484

485+
result = self.frame.transform(np.sqrt)
486+
assert_frame_equal(result, expected)
487+
485488
# list-like
486489
result = self.frame.apply([np.sqrt])
487490
expected = f_sqrt.copy()

pandas/tests/series/test_apply.py

+4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ def test_demo(self):
234234
expected = DataFrame({'foo': [0, 5]}, index=['min', 'max'])
235235
tm.assert_frame_equal(result, expected)
236236

237+
def test_multiple_aggregators_with_dict_api(self):
238+
239+
s = Series(range(6), dtype='int64')
237240
result = s.agg({'foo': ['min', 'max'], 'bar': ['sum', 'mean']})
241+
238242
expected = DataFrame({'foo': [5.0, np.nan, 0.0, np.nan],
239243
'bar': [np.nan, 2.5, np.nan, 15.0]},
240244
columns=['foo', 'bar'],

0 commit comments

Comments
 (0)