@@ -331,13 +331,14 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs):
331
331
332
332
raise ValueError ("{arg} is an unknown string function" .format (arg = arg ))
333
333
334
- def _aggregate (self , arg , * args , ** kwargs ):
334
+ def _aggregate (self , arg , axis = 0 , * args , ** kwargs ):
335
335
"""
336
336
provide an implementation for the aggregators
337
337
338
338
Parameters
339
339
----------
340
340
arg : string, dict, function
341
+ axis : int
341
342
*args : args to pass on to the function
342
343
**kwargs : kwargs to pass on to the function
343
344
@@ -350,25 +351,26 @@ def _aggregate(self, arg, *args, **kwargs):
350
351
how can be a string describe the required post-processing, or
351
352
None if not required
352
353
"""
354
+ obj = self if axis == 0 else self .T
353
355
is_aggregator = lambda x : isinstance (x , (list , tuple , dict ))
354
356
is_nested_renamer = False
355
357
356
358
_axis = kwargs .pop ('_axis' , None )
357
359
if _axis is None :
358
- _axis = getattr (self , 'axis' , 0 )
360
+ _axis = getattr (obj , 'axis' , 0 )
359
361
_level = kwargs .pop ('_level' , None )
360
362
361
363
if isinstance (arg , compat .string_types ):
362
- return self ._try_aggregate_string_function (arg , * args ,
363
- ** kwargs ), None
364
+ return obj ._try_aggregate_string_function (arg , * args ,
365
+ ** kwargs ), None
364
366
365
367
if isinstance (arg , dict ):
366
368
367
369
# aggregate based on the passed dict
368
370
if _axis != 0 : # pragma: no cover
369
371
raise ValueError ('Can only pass dict with axis=0' )
370
372
371
- obj = self ._selected_obj
373
+ selected_obj = obj ._selected_obj
372
374
373
375
def nested_renaming_depr (level = 4 ):
374
376
# deprecation of nested renaming
@@ -403,16 +405,16 @@ def nested_renaming_depr(level=4):
403
405
if isinstance (v , dict ):
404
406
is_nested_renamer = True
405
407
406
- if k not in obj .columns :
408
+ if k not in selected_obj .columns :
407
409
msg = ('cannot perform renaming for {key} with a '
408
410
'nested dictionary' ).format (key = k )
409
411
raise SpecificationError (msg )
410
412
nested_renaming_depr (4 + (_level or 0 ))
411
413
412
- elif isinstance (obj , ABCSeries ):
414
+ elif isinstance (selected_obj , ABCSeries ):
413
415
nested_renaming_depr ()
414
- elif isinstance (obj , ABCDataFrame ) and \
415
- k not in obj .columns :
416
+ elif isinstance (selected_obj , ABCDataFrame ) and \
417
+ k not in selected_obj .columns :
416
418
raise KeyError (
417
419
"Column '{col}' does not exist!" .format (col = k ))
418
420
@@ -422,8 +424,9 @@ def nested_renaming_depr(level=4):
422
424
# deprecation of renaming keys
423
425
# GH 15931
424
426
keys = list (compat .iterkeys (arg ))
425
- if (isinstance (obj , ABCDataFrame ) and
426
- len (obj .columns .intersection (keys )) != len (keys )):
427
+ intersection_keys = selected_obj .columns .intersection (keys )
428
+ if (isinstance (selected_obj , ABCDataFrame ) and
429
+ len (intersection_keys ) != len (keys )):
427
430
nested_renaming_depr ()
428
431
429
432
from pandas .core .reshape .concat import concat
@@ -432,7 +435,7 @@ def _agg_1dim(name, how, subset=None):
432
435
"""
433
436
aggregate a 1-dim with how
434
437
"""
435
- colg = self ._gotitem (name , ndim = 1 , subset = subset )
438
+ colg = obj ._gotitem (name , ndim = 1 , subset = subset )
436
439
if colg .ndim != 1 :
437
440
raise SpecificationError ("nested dictionary is ambiguous "
438
441
"in aggregation" )
@@ -442,8 +445,8 @@ def _agg_2dim(name, how):
442
445
"""
443
446
aggregate a 2-dim with how
444
447
"""
445
- colg = self ._gotitem (self ._selection , ndim = 2 ,
446
- subset = obj )
448
+ colg = obj ._gotitem (obj ._selection , ndim = 2 ,
449
+ subset = selected_obj )
447
450
return colg .aggregate (how , _level = None )
448
451
449
452
def _agg (arg , func ):
@@ -473,20 +476,21 @@ def _agg(arg, func):
473
476
474
477
else :
475
478
476
- if self ._selection is not None :
479
+ if obj ._selection is not None :
477
480
keys = None
478
481
479
482
# some selection on the object
480
- elif self ._selection is not None :
483
+ elif obj ._selection is not None :
481
484
482
- sl = set (self ._selection_list )
485
+ sl = set (obj ._selection_list )
483
486
484
487
# we are a Series like object,
485
488
# but may have multiple aggregations
486
489
if len (sl ) == 1 :
487
490
488
491
result = _agg (arg , lambda fname ,
489
- agg_how : _agg_1dim (self ._selection , agg_how ))
492
+ agg_how : _agg_1dim (
493
+ obj ._selection , agg_how ))
490
494
491
495
# we are selecting the same set as we are aggregating
492
496
elif not len (sl - set (keys )):
@@ -531,7 +535,7 @@ def is_any_frame():
531
535
return concat ([result [k ] for k in keys ],
532
536
keys = keys , axis = 1 ), True
533
537
534
- elif isinstance (self , ABCSeries ) and is_any_series ():
538
+ elif isinstance (obj , ABCSeries ) and is_any_series ():
535
539
536
540
# we have a dict of Series
537
541
# return a MI Series
@@ -556,20 +560,20 @@ def is_any_frame():
556
560
557
561
# we have a dict of scalars
558
562
result = Series (result ,
559
- name = getattr (self , 'name' , None ))
563
+ name = getattr (obj , 'name' , None ))
560
564
561
565
return result , True
562
566
elif is_list_like (arg ) and arg not in compat .string_types :
563
567
# we require a list, but not an 'str'
564
- return self ._aggregate_multiple_funcs (arg ,
565
- _level = _level ,
566
- _axis = _axis ), None
568
+ return obj ._aggregate_multiple_funcs (arg ,
569
+ _level = _level ,
570
+ _axis = _axis ), None
567
571
else :
568
572
result = None
569
573
570
- f = self ._is_cython_func (arg )
574
+ f = obj ._is_cython_func (arg )
571
575
if f is not None :
572
- return getattr (self , f )(* args , ** kwargs ), None
576
+ return getattr (obj , f )(* args , ** kwargs ), None
573
577
574
578
# caller can react
575
579
return result , True
0 commit comments