@@ -316,13 +316,14 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs):
316
316
317
317
raise ValueError ("{arg} is an unknown string function" .format (arg = arg ))
318
318
319
- def _aggregate (self , arg , * args , ** kwargs ):
319
+ def _aggregate (self , arg , axis = 0 , * args , ** kwargs ):
320
320
"""
321
321
provide an implementation for the aggregators
322
322
323
323
Parameters
324
324
----------
325
325
arg : string, dict, function
326
+ axis : int
326
327
*args : args to pass on to the function
327
328
**kwargs : kwargs to pass on to the function
328
329
@@ -335,25 +336,26 @@ def _aggregate(self, arg, *args, **kwargs):
335
336
how can be a string describe the required post-processing, or
336
337
None if not required
337
338
"""
339
+ obj = self if axis == 0 else self .T
338
340
is_aggregator = lambda x : isinstance (x , (list , tuple , dict ))
339
341
is_nested_renamer = False
340
342
341
343
_axis = kwargs .pop ('_axis' , None )
342
344
if _axis is None :
343
- _axis = getattr (self , 'axis' , 0 )
345
+ _axis = getattr (obj , 'axis' , 0 )
344
346
_level = kwargs .pop ('_level' , None )
345
347
346
348
if isinstance (arg , compat .string_types ):
347
- return self ._try_aggregate_string_function (arg , * args ,
348
- ** kwargs ), None
349
+ return obj ._try_aggregate_string_function (arg , * args ,
350
+ ** kwargs ), None
349
351
350
352
if isinstance (arg , dict ):
351
353
352
354
# aggregate based on the passed dict
353
355
if _axis != 0 : # pragma: no cover
354
356
raise ValueError ('Can only pass dict with axis=0' )
355
357
356
- obj = self ._selected_obj
358
+ selected_obj = obj ._selected_obj
357
359
358
360
def nested_renaming_depr (level = 4 ):
359
361
# deprecation of nested renaming
@@ -388,16 +390,16 @@ def nested_renaming_depr(level=4):
388
390
if isinstance (v , dict ):
389
391
is_nested_renamer = True
390
392
391
- if k not in obj .columns :
393
+ if k not in selected_obj .columns :
392
394
msg = ('cannot perform renaming for {key} with a '
393
395
'nested dictionary' ).format (key = k )
394
396
raise SpecificationError (msg )
395
397
nested_renaming_depr (4 + (_level or 0 ))
396
398
397
- elif isinstance (obj , ABCSeries ):
399
+ elif isinstance (selected_obj , ABCSeries ):
398
400
nested_renaming_depr ()
399
- elif isinstance (obj , ABCDataFrame ) and \
400
- k not in obj .columns :
401
+ elif isinstance (selected_obj , ABCDataFrame ) and \
402
+ k not in selected_obj .columns :
401
403
raise KeyError (
402
404
"Column '{col}' does not exist!" .format (col = k ))
403
405
@@ -407,8 +409,8 @@ def nested_renaming_depr(level=4):
407
409
# deprecation of renaming keys
408
410
# GH 15931
409
411
keys = list (compat .iterkeys (arg ))
410
- if (isinstance (obj , ABCDataFrame ) and
411
- len ( obj .columns .intersection (keys )) != len (keys )):
412
+ if (isinstance (selected_obj , ABCDataFrame ) and len (
413
+ selected_obj .columns .intersection (keys )) != len (keys )):
412
414
nested_renaming_depr ()
413
415
414
416
from pandas .core .reshape .concat import concat
@@ -417,7 +419,7 @@ def _agg_1dim(name, how, subset=None):
417
419
"""
418
420
aggregate a 1-dim with how
419
421
"""
420
- colg = self ._gotitem (name , ndim = 1 , subset = subset )
422
+ colg = obj ._gotitem (name , ndim = 1 , subset = subset )
421
423
if colg .ndim != 1 :
422
424
raise SpecificationError ("nested dictionary is ambiguous "
423
425
"in aggregation" )
@@ -427,8 +429,8 @@ def _agg_2dim(name, how):
427
429
"""
428
430
aggregate a 2-dim with how
429
431
"""
430
- colg = self ._gotitem (self ._selection , ndim = 2 ,
431
- subset = obj )
432
+ colg = obj ._gotitem (obj ._selection , ndim = 2 ,
433
+ subset = selected_obj )
432
434
return colg .aggregate (how , _level = None )
433
435
434
436
def _agg (arg , func ):
@@ -458,20 +460,22 @@ def _agg(arg, func):
458
460
459
461
else :
460
462
461
- if self ._selection is not None :
463
+ if obj ._selection is not None :
462
464
keys = None
463
465
464
466
# some selection on the object
465
- elif self ._selection is not None :
467
+ elif obj ._selection is not None :
466
468
467
- sl = set (self ._selection_list )
469
+ sl = set (obj ._selection_list )
468
470
469
471
# we are a Series like object,
470
472
# but may have multiple aggregations
471
473
if len (sl ) == 1 :
472
474
473
- result = _agg (arg , lambda fname ,
474
- agg_how : _agg_1dim (self ._selection , agg_how ))
475
+ result = _agg (
476
+ arg ,
477
+ lambda fname , agg_how : _agg_1dim (
478
+ obj ._selection , agg_how ))
475
479
476
480
# we are selecting the same set as we are aggregating
477
481
elif not len (sl - set (keys )):
@@ -516,7 +520,7 @@ def is_any_frame():
516
520
return concat ([result [k ] for k in keys ],
517
521
keys = keys , axis = 1 ), True
518
522
519
- elif isinstance (self , ABCSeries ) and is_any_series ():
523
+ elif isinstance (obj , ABCSeries ) and is_any_series ():
520
524
521
525
# we have a dict of Series
522
526
# return a MI Series
@@ -541,20 +545,20 @@ def is_any_frame():
541
545
542
546
# we have a dict of scalars
543
547
result = Series (result ,
544
- name = getattr (self , 'name' , None ))
548
+ name = getattr (obj , 'name' , None ))
545
549
546
550
return result , True
547
551
elif is_list_like (arg ) and arg not in compat .string_types :
548
552
# we require a list, but not an 'str'
549
- return self ._aggregate_multiple_funcs (arg ,
550
- _level = _level ,
551
- _axis = _axis ), None
553
+ return obj ._aggregate_multiple_funcs (arg ,
554
+ _level = _level ,
555
+ _axis = _axis ), None
552
556
else :
553
557
result = None
554
558
555
- f = self ._is_cython_func (arg )
556
- if f and not args and not kwargs :
557
- return getattr (self , f )(), None
559
+ f = obj ._is_cython_func (arg )
560
+ if f is not None :
561
+ return getattr (obj , f )(* args , ** kwargs ), None
558
562
559
563
# caller can react
560
564
return result , True
0 commit comments