Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
add alt api for series agg with dict
  • Loading branch information
jreback committed Dec 13, 2016
1 parent a3deeaf commit d3bc362
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
16 changes: 12 additions & 4 deletions doc/source/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ Aggregation API

.. versionadded:: 0.20.0

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

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

.. ipython:: python
Expand Down Expand Up @@ -915,6 +918,12 @@ For a Series, you can pass a dict; the keys will set the name of the column
tsdf.A.agg({'foo' : ['sum', 'mean']})
Alternatively, using multiple dictionaries, you can have renamed elements with the aggregation

.. ipython:: python
tsdf.A.agg({'foo' : 'sum', 'bar':'mean'})
Multiple keys will yield multiple columns.

.. ipython:: python
Expand Down Expand Up @@ -969,12 +978,11 @@ function name and user defined function.
tsdf.transform('abs')
tsdf.transform(lambda x: x.abs())
``.transform()`` with a single function is equivalent to applying a function across the
columns.
Since this is a single function, this is equivalent to a ufunc application

.. ipython:: python
tsdf.apply(np.abs, axis=1)
np.abs(tsdf)
Passing a single function to ``.transform()`` with a Series will yield a single Series in return.

Expand Down
19 changes: 12 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2782,17 +2782,16 @@ def pipe(self, func, *args, **kwargs):
Parameters
----------
arg : function or dict
func : callable, string, dictionary, or list of string/callables
Function to use for aggregating the data. If a function, must either
work when passed a DataFrame or when passed to DataFrame.apply. If
passed a dict, the keys must be DataFrame column names.
Accepted Combinations are:
- string cythonized function name
- function
- list of functions
- dict of columns -> functions
- nested dict of names -> dicts of functions
- string function name
- function
- list of functions
- dict of column names -> functions (or list of functions)
Notes
-----
Expand All @@ -2817,9 +2816,15 @@ def pipe(self, func, *args, **kwargs):
Parameters
----------
func : function, callable or string
func : callable, string, dictionary, or list of string/callables
To apply to column
Accepted Combinations are:
- string function name
- function
- list of functions
- dict of column names -> functions (or list of functions)
Examples
--------
>>> df.transform(lambda x: (x - x.mean()) / x.std())
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ def test_agg_transform(self):
result = self.frame.apply(np.sqrt)
assert_frame_equal(result, expected)

result = self.frame.transform(np.sqrt)
assert_frame_equal(result, expected)

# list-like
result = self.frame.apply([np.sqrt])
expected = f_sqrt.copy()
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ def test_demo(self):
expected = DataFrame({'foo': [0, 5]}, index=['min', 'max'])
tm.assert_frame_equal(result, expected)

def test_multiple_aggregators_with_dict_api(self):

s = Series(range(6), dtype='int64')
result = s.agg({'foo': ['min', 'max'], 'bar': ['sum', 'mean']})

expected = DataFrame({'foo': [5.0, np.nan, 0.0, np.nan],
'bar': [np.nan, 2.5, np.nan, 15.0]},
columns=['foo', 'bar'],
Expand Down

0 comments on commit d3bc362

Please sign in to comment.