Deprecation of using a dict in groupby().agg({...}) to rename resulting columns

2,891 views
Skip to first unread message

Joris Van den Bossche

unread,
Apr 12, 2017, 9:20:59 AM4/12/17
to panda...@python.org, PyData
Hi all,

As a heads up, we are deprecating the use of a dictionary in the agg method on groupby/resample/rolling objects.

Basic example:

In [1]: df = pd.DataFrame({'A': [1, 1, 1, 2, 2],
   ...:                    'B': range(5),
   ...:                    'C':range(5)})
In [3]: df.groupby('A').B.agg({'foo': 'count'})
Out[3]: 
   foo
A     
1    3
2    2
The above will be deprecated (and removed in a future version).

The documentation added in https://github.com/pandas-dev/pandas/pull/15931 gives a more detailed explanation of the reasoning, but basically it is to clean up the API: now you can use dicts both to specify a aggregation function for a specific column (for dataframes) as to rename the result (series, or dataframes with nested dict), which is a confusing double use of dicts.

The PR will be merged shortly, but the 0.20 release is still a few weeks off, so certainly still time to give feedback. Which is certainly welcome!

Regards,
Joris

Rennie Archibald

unread,
Jul 2, 2018, 10:20:03 PM7/2/18
to PyData
Is there an example of how we can rename columns now?

Paul Hobson

unread,
Jul 3, 2018, 12:32:26 AM7/3/18
to pyd...@googlegroups.com
Rennie,

I would do:
 df.groupby('A').count().rename(columns={'B': 'foo'})


--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages