Skip to content

Commit a63da2d

Browse files
committed
give proper message for Dataframe with renaming keys
1 parent 44f61ba commit a63da2d

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

pandas/core/base.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ def _aggregate(self, arg, *args, **kwargs):
447447
how can be a string describe the required post-processing, or
448448
None if not required
449449
"""
450-
451450
is_aggregator = lambda x: isinstance(x, (list, tuple, dict))
452451
is_nested_renamer = False
453452

@@ -482,7 +481,7 @@ def _aggregate(self, arg, *args, **kwargs):
482481
# the keys must be in the columns
483482
# for ndim=2, or renamers for ndim=1
484483

485-
# ok
484+
# ok for now, but deprecated
486485
# {'A': { 'ra': 'mean' }}
487486
# {'A': { 'ra': ['mean'] }}
488487
# {'ra': ['mean']}
@@ -497,8 +496,26 @@ def _aggregate(self, arg, *args, **kwargs):
497496
'for {0} with a nested '
498497
'dictionary'.format(k))
499498

499+
# deprecation of nested renaming
500+
warnings.warn(
501+
("using a dict with renaming "
502+
"is deprecated and will be removed in a future "
503+
"version"),
504+
FutureWarning, stacklevel=3)
505+
500506
arg = new_arg
501507

508+
else:
509+
# we may have renaming keys
510+
keys = list(compat.iterkeys(arg))
511+
if (isinstance(obj, ABCDataFrame) and
512+
len(obj.columns.intersection(keys)) != len(keys)):
513+
warnings.warn(
514+
("using a dict with renaming "
515+
"is deprecated and will be removed in a future "
516+
"version"),
517+
FutureWarning, stacklevel=3)
518+
502519
from pandas.tools.concat import concat
503520

504521
def _agg_1dim(name, how, subset=None):
@@ -533,16 +550,6 @@ def _agg(arg, func):
533550
keys = list(compat.iterkeys(arg))
534551
result = compat.OrderedDict()
535552

536-
# renaming keys
537-
if isinstance(self._selected_obj, ABCDataFrame):
538-
if len(self._selected_obj.columns.intersection(
539-
keys)) != len(keys):
540-
warnings.warn(
541-
("using a dict with renaming"
542-
"is deprecated and will be removed in a future "
543-
"version"),
544-
FutureWarning, stacklevel=3)
545-
546553
# nested renamer
547554
if is_nested_renamer:
548555
result = list(_agg(arg, _agg_1dim).values())

pandas/core/groupby.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2835,9 +2835,11 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
28352835
def _aggregate_multiple_funcs(self, arg, _level):
28362836
if isinstance(arg, dict):
28372837

2838-
if isinstance(self._selected_obj, Series):
2838+
# show the deprecation, but only if we
2839+
# have not shown a higher level one
2840+
if isinstance(self._selected_obj, Series) and _level <= 1:
28392841
warnings.warn(
2840-
("using a dictionary on a Series for aggregation\n"
2842+
("using a dict on a Series for aggregation\n"
28412843
"is deprecated and will be removed in a future "
28422844
"version"),
28432845
FutureWarning, stacklevel=7)

pandas/tests/groupby/test_aggregate.py

+18
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,24 @@ def test_aggregate_api_consistency(self):
304304
['D', 'C']])
305305
assert_frame_equal(result, expected, check_like=True)
306306

307+
def test_agg_dict_renaming_deprecation(self):
308+
# 15931
309+
df = pd.DataFrame({'A': [1, 1, 1, 2, 2],
310+
'B': range(5),
311+
'C': range(5)})
312+
313+
with tm.assert_produces_warning(FutureWarning,
314+
check_stacklevel=False) as w:
315+
df.groupby('A').agg({'B': {'foo': ['sum', 'max']},
316+
'C': {'bar': ['count', 'min']}})
317+
assert "using a dict with renaming" in str(w[0].message)
318+
319+
with tm.assert_produces_warning(FutureWarning,
320+
check_stacklevel=False) as w:
321+
df.groupby('A').B.agg({'foo': 'count'})
322+
assert "using a dict on a Series for aggregation" in str(
323+
w[0].message)
324+
307325
def test_agg_compat(self):
308326

309327
# GH 12334

0 commit comments

Comments
 (0)