Skip to content

DEPR: deprecate relableling dicts in groupby.agg #15931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
update docs per review
  • Loading branch information
jreback committed Apr 12, 2017
commit ff1a5f66fe4f7a225945e5a12c2b8063e163f954
8 changes: 0 additions & 8 deletions doc/source/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -610,14 +610,6 @@ aggregation with, outputting a DataFrame:

r['A'].agg([np.sum, np.mean, np.std])

If a dict is passed, the keys will be used to name the columns. Otherwise the
function's name (stored in the function object) will be used.

.. ipython:: python

r['A'].agg({'result1' : np.sum,
'result2' : np.mean})

On a widowed DataFrame, you can pass a list of functions to apply to each
column, which produces an aggregated result with a hierarchical index:

Expand Down
32 changes: 22 additions & 10 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,31 +502,43 @@ index are the group names and whose values are the sizes of each group.
Applying multiple functions at once
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With grouped Series you can also pass a list or dict of functions to do
With grouped ``Series`` you can also pass a list or dict of functions to do
aggregation with, outputting a DataFrame:

.. ipython:: python

grouped = df.groupby('A')
grouped['C'].agg([np.sum, np.mean, np.std])

If a dict is passed, the keys will be used to name the columns. Otherwise the
function's name (stored in the function object) will be used.
On a grouped ``DataFrame``, you can pass a list of functions to apply to each
column, which produces an aggregated result with a hierarchical index:

.. ipython:: python

grouped['D'].agg({'result1' : np.sum,
'result2' : np.mean})
grouped.agg([np.sum, np.mean, np.std])

On a grouped DataFrame, you can pass a list of functions to apply to each
column, which produces an aggregated result with a hierarchical index:

The resulting aggregations are named for the functions themselves. If you
need to rename, then you can add in a chained operation for a ``Series`` like this:

.. ipython:: python

grouped.agg([np.sum, np.mean, np.std])
(grouped['C'].agg([np.sum, np.mean, np.std])
.rename(columns={'sum': 'foo',
'mean': 'bar',
'std': 'baz'})
)

For a grouped ``DataFrame``, you can rename in a similar manner:

.. ipython:: python

(grouped.agg([np.sum, np.mean, np.std])
.rename(columns={'sum': 'foo',
'mean': 'bar',
'std': 'baz'})
)

Passing a dict of functions has different behavior by default, see the next
section.

Applying different functions to DataFrame columns
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
8 changes: 0 additions & 8 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1549,14 +1549,6 @@ You can pass a list or dict of functions to do aggregation with, outputting a Da

r['A'].agg([np.sum, np.mean, np.std])

If a dict is passed, the keys will be used to name the columns. Otherwise the
function's name (stored in the function object) will be used.

.. ipython:: python

r['A'].agg({'result1' : np.sum,
'result2' : np.mean})

On a resampled DataFrame, you can pass a list of functions to apply to each
column, which produces an aggregated result with a hierarchical index:

Expand Down
35 changes: 18 additions & 17 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,12 @@ list, and a dict of column names to scalars or lists. This provides a useful syn
(potentially different) aggregations.

However, ``.agg(..)`` can *also* accept a dict that allows 'renaming' of the result columns. This is a complicated and confusing syntax, as well as not consistent
between ``Series`` and ``DataFrame``. We are deprecating this 'renaming' functionarility.
between ``Series`` and ``DataFrame``. We are deprecating this 'renaming' functionaility.

1) We are deprecating passing a dict to a grouped/rolled/resampled ``Series``. This allowed
one to ``rename`` the resulting aggregation, but this had a completely different
meaning than passing a dictionary to a grouped ``DataFrame``, which accepts column-to-aggregations.
2) We are deprecating passing a dict-of-dict to a grouped/rolled/resampled ``DataFrame`` in a similar manner.
2) We are deprecating passing a dict-of-dicts to a grouped/rolled/resampled ``DataFrame`` in a similar manner.

This is an illustrative example:

Expand All @@ -488,17 +488,15 @@ columns and applying the list of functions. This returns a ``MultiIndex`` for th

.. ipython:: python

df.groupby('A').agg({'B': ['sum', 'max'],
'C': ['count', 'min']})

df.groupby('A').agg({'B': 'sum', 'C': 'min'})

Here's an example of the first deprecation (1), passing a dict to a grouped ``Series``. This
is a combination aggregation & renaming:

.. code-block:: ipython

In [6]: df.groupby('A').B.agg({'foo': 'count'})
FutureWarning: using a dictionary on a Series for aggregation
FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version

Out[6]:
Expand All @@ -518,24 +516,27 @@ Here's an example of the second deprecation (2), passing a dict-of-dict to a gro

.. code-block:: python

In [23]: df.groupby('A').agg({'B': {'foo': ['sum', 'max']}, 'C': {'bar': ['count', 'min']}})
FutureWarning: using a dictionary on a Series for aggregation
is deprecated and will be removed in a future version
In [23]: (df.groupby('A')
.agg({'B': {'foo': 'sum'}, 'C': {'bar': 'min'}})
)
FutureWarning: using a dict with renaming is deprecated and will be removed in a future version

Out[23]:
foo bar
sum max count min
B C
foo bar
A
1 3 2 3 0
2 7 4 2 3
1 3 0
2 7 3

You can accomplish the same by:

You can accomplish nearly the same by:

.. ipython:: python

r = df.groupby('A').agg({'B': ['sum', 'max'], 'C': ['count', 'min']})
r.columns = r.columns.set_levels(['foo', 'bar'], level=0)
r
(df.groupby('A')
.agg({'B': 'sum', 'C': 'min'})
.rename(columns={'B': 'foo', 'C': 'bar'})
)

.. _whatsnew.api_breaking.io_compat:

Expand Down
11 changes: 7 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ class SelectionMixin(object):

@property
def _selection_name(self):
""" return a name for myself; this would ideally be the 'name' property, but
we cannot conflict with the Series.name property which can be set """
"""
return a name for myself; this would ideally be called
the 'name' property, but we cannot conflict with the
Series.name property which can be set
"""
if self._selection is None:
return None # 'result'
else:
Expand Down Expand Up @@ -502,7 +505,7 @@ def _aggregate(self, arg, *args, **kwargs):
("using a dict with renaming "
"is deprecated and will be removed in a future "
"version"),
FutureWarning, stacklevel=3)
FutureWarning, stacklevel=4)

arg = new_arg

Expand All @@ -516,7 +519,7 @@ def _aggregate(self, arg, *args, **kwargs):
("using a dict with renaming "
"is deprecated and will be removed in a future "
"version"),
FutureWarning, stacklevel=3)
FutureWarning, stacklevel=4)

from pandas.tools.concat import concat

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,7 @@ def _aggregate_multiple_funcs(self, arg, _level):
("using a dict on a Series for aggregation\n"
"is deprecated and will be removed in a future "
"version"),
FutureWarning, stacklevel=7)
FutureWarning, stacklevel=4)

columns = list(arg.keys())
arg = list(arg.items())
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/groupby/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ def test_agg_dict_renaming_deprecation(self):
'C': {'bar': ['count', 'min']}})
assert "using a dict with renaming" in str(w[0].message)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is tested in other cases as well, but since this is a test specifically for the deprs, maybe also add the case of df.groupby('A')[['B', 'C']].agg({'ma': 'max'}) (then you have the different 'cases' that raise deprecation here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
df.groupby('A')[['B', 'C']].agg({'ma': 'max'})

with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False) as w:
df.groupby('A').B.agg({'foo': 'count'})
Expand Down