Skip to content

Commit ff1a5f6

Browse files
committed
update docs per review
1 parent 7262515 commit ff1a5f6

File tree

7 files changed

+52
-48
lines changed

7 files changed

+52
-48
lines changed

doc/source/computation.rst

-8
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,6 @@ aggregation with, outputting a DataFrame:
610610
611611
r['A'].agg([np.sum, np.mean, np.std])
612612
613-
If a dict is passed, the keys will be used to name the columns. Otherwise the
614-
function's name (stored in the function object) will be used.
615-
616-
.. ipython:: python
617-
618-
r['A'].agg({'result1' : np.sum,
619-
'result2' : np.mean})
620-
621613
On a widowed DataFrame, you can pass a list of functions to apply to each
622614
column, which produces an aggregated result with a hierarchical index:
623615

doc/source/groupby.rst

+22-10
Original file line numberDiff line numberDiff line change
@@ -502,31 +502,43 @@ index are the group names and whose values are the sizes of each group.
502502
Applying multiple functions at once
503503
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
504504

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

508508
.. ipython:: python
509509
510510
grouped = df.groupby('A')
511511
grouped['C'].agg([np.sum, np.mean, np.std])
512512
513-
If a dict is passed, the keys will be used to name the columns. Otherwise the
514-
function's name (stored in the function object) will be used.
513+
On a grouped ``DataFrame``, you can pass a list of functions to apply to each
514+
column, which produces an aggregated result with a hierarchical index:
515515

516516
.. ipython:: python
517517
518-
grouped['D'].agg({'result1' : np.sum,
519-
'result2' : np.mean})
518+
grouped.agg([np.sum, np.mean, np.std])
520519
521-
On a grouped DataFrame, you can pass a list of functions to apply to each
522-
column, which produces an aggregated result with a hierarchical index:
520+
521+
The resulting aggregations are named for the functions themselves. If you
522+
need to rename, then you can add in a chained operation for a ``Series`` like this:
523523

524524
.. ipython:: python
525525
526-
grouped.agg([np.sum, np.mean, np.std])
526+
(grouped['C'].agg([np.sum, np.mean, np.std])
527+
.rename(columns={'sum': 'foo',
528+
'mean': 'bar',
529+
'std': 'baz'})
530+
)
531+
532+
For a grouped ``DataFrame``, you can rename in a similar manner:
533+
534+
.. ipython:: python
535+
536+
(grouped.agg([np.sum, np.mean, np.std])
537+
.rename(columns={'sum': 'foo',
538+
'mean': 'bar',
539+
'std': 'baz'})
540+
)
527541
528-
Passing a dict of functions has different behavior by default, see the next
529-
section.
530542
531543
Applying different functions to DataFrame columns
532544
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/source/timeseries.rst

-8
Original file line numberDiff line numberDiff line change
@@ -1549,14 +1549,6 @@ You can pass a list or dict of functions to do aggregation with, outputting a Da
15491549
15501550
r['A'].agg([np.sum, np.mean, np.std])
15511551
1552-
If a dict is passed, the keys will be used to name the columns. Otherwise the
1553-
function's name (stored in the function object) will be used.
1554-
1555-
.. ipython:: python
1556-
1557-
r['A'].agg({'result1' : np.sum,
1558-
'result2' : np.mean})
1559-
15601552
On a resampled DataFrame, you can pass a list of functions to apply to each
15611553
column, which produces an aggregated result with a hierarchical index:
15621554

doc/source/whatsnew/v0.20.0.txt

+18-17
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ list, and a dict of column names to scalars or lists. This provides a useful syn
466466
(potentially different) aggregations.
467467

468468
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
469-
between ``Series`` and ``DataFrame``. We are deprecating this 'renaming' functionarility.
469+
between ``Series`` and ``DataFrame``. We are deprecating this 'renaming' functionaility.
470470

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

476476
This is an illustrative example:
477477

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

489489
.. ipython:: python
490490

491-
df.groupby('A').agg({'B': ['sum', 'max'],
492-
'C': ['count', 'min']})
493-
491+
df.groupby('A').agg({'B': 'sum', 'C': 'min'})
494492

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

498496
.. code-block:: ipython
499497

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

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

519517
.. code-block:: python
520518

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

525524
Out[23]:
526-
foo bar
527-
sum max count min
525+
B C
526+
foo bar
528527
A
529-
1 3 2 3 0
530-
2 7 4 2 3
528+
1 3 0
529+
2 7 3
531530

532-
You can accomplish the same by:
531+
532+
You can accomplish nearly the same by:
533533

534534
.. ipython:: python
535535

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

540541
.. _whatsnew.api_breaking.io_compat:
541542

pandas/core/base.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,11 @@ class SelectionMixin(object):
292292

293293
@property
294294
def _selection_name(self):
295-
""" return a name for myself; this would ideally be the 'name' property, but
296-
we cannot conflict with the Series.name property which can be set """
295+
"""
296+
return a name for myself; this would ideally be called
297+
the 'name' property, but we cannot conflict with the
298+
Series.name property which can be set
299+
"""
297300
if self._selection is None:
298301
return None # 'result'
299302
else:
@@ -502,7 +505,7 @@ def _aggregate(self, arg, *args, **kwargs):
502505
("using a dict with renaming "
503506
"is deprecated and will be removed in a future "
504507
"version"),
505-
FutureWarning, stacklevel=3)
508+
FutureWarning, stacklevel=4)
506509

507510
arg = new_arg
508511

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

521524
from pandas.tools.concat import concat
522525

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2843,7 +2843,7 @@ def _aggregate_multiple_funcs(self, arg, _level):
28432843
("using a dict on a Series for aggregation\n"
28442844
"is deprecated and will be removed in a future "
28452845
"version"),
2846-
FutureWarning, stacklevel=7)
2846+
FutureWarning, stacklevel=4)
28472847

28482848
columns = list(arg.keys())
28492849
arg = list(arg.items())

pandas/tests/groupby/test_aggregate.py

+4
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ def test_agg_dict_renaming_deprecation(self):
316316
'C': {'bar': ['count', 'min']}})
317317
assert "using a dict with renaming" in str(w[0].message)
318318

319+
with tm.assert_produces_warning(FutureWarning,
320+
check_stacklevel=False):
321+
df.groupby('A')[['B', 'C']].agg({'ma': 'max'})
322+
319323
with tm.assert_produces_warning(FutureWarning,
320324
check_stacklevel=False) as w:
321325
df.groupby('A').B.agg({'foo': 'count'})

0 commit comments

Comments
 (0)