@@ -11,6 +11,8 @@ class providing the base-class of operations.
11
11
from contextlib import contextmanager
12
12
import datetime
13
13
from functools import partial , wraps
14
+ import inspect
15
+ import re
14
16
import types
15
17
from typing import Dict , FrozenSet , List , Optional , Tuple , Type , Union
16
18
@@ -613,48 +615,53 @@ def _make_wrapper(self, name):
613
615
return self .apply (lambda self : getattr (self , name ))
614
616
615
617
f = getattr (type (self ._selected_obj ), name )
618
+ sig = inspect .signature (f )
616
619
617
620
def wrapper (* args , ** kwargs ):
618
621
# a little trickery for aggregation functions that need an axis
619
622
# argument
620
- kwargs_with_axis = kwargs .copy ()
621
- if "axis" not in kwargs_with_axis or kwargs_with_axis ["axis" ] is None :
622
- kwargs_with_axis ["axis" ] = self .axis
623
-
624
- def curried_with_axis (x ):
625
- return f (x , * args , ** kwargs_with_axis )
623
+ if "axis" in sig .parameters :
624
+ if kwargs .get ("axis" , None ) is None :
625
+ kwargs ["axis" ] = self .axis
626
626
627
627
def curried (x ):
628
628
return f (x , * args , ** kwargs )
629
629
630
630
# preserve the name so we can detect it when calling plot methods,
631
631
# to avoid duplicates
632
- curried .__name__ = curried_with_axis . __name__ = name
632
+ curried .__name__ = name
633
633
634
634
# special case otherwise extra plots are created when catching the
635
635
# exception below
636
636
if name in base .plotting_methods :
637
637
return self .apply (curried )
638
638
639
639
try :
640
- return self .apply (curried_with_axis )
641
- except Exception :
642
- try :
643
- return self .apply (curried )
644
- except Exception :
645
-
646
- # related to : GH3688
647
- # try item-by-item
648
- # this can be called recursively, so need to raise
649
- # ValueError
650
- # if we don't have this method to indicated to aggregate to
651
- # mark this column as an error
652
- try :
653
- return self ._aggregate_item_by_item (name , * args , ** kwargs )
654
- except AttributeError :
655
- # e.g. SparseArray has no flags attr
656
- raise ValueError
657
-
640
+ return self .apply (curried )
641
+ except TypeError as err :
642
+ if not re .search (
643
+ "reduction operation '.*' not allowed for this dtype" , str (err )
644
+ ):
645
+ # We don't have a cython implementation
646
+ # TODO: is the above comment accurate?
647
+ raise
648
+
649
+ # related to : GH3688
650
+ # try item-by-item
651
+ # this can be called recursively, so need to raise
652
+ # ValueError
653
+ # if we don't have this method to indicated to aggregate to
654
+ # mark this column as an error
655
+ try :
656
+ return self ._aggregate_item_by_item (name , * args , ** kwargs )
657
+ except AttributeError :
658
+ # e.g. SparseArray has no flags attr
659
+ # FIXME: 'SeriesGroupBy' has no attribute '_aggregate_item_by_item'
660
+ # occurs in idxmax() case
661
+ # in tests.groupby.test_function.test_non_cython_api
662
+ raise ValueError
663
+
664
+ wrapper .__name__ = name
658
665
return wrapper
659
666
660
667
def get_group (self , name , obj = None ):
@@ -747,7 +754,7 @@ def _python_apply_general(self, f):
747
754
)
748
755
749
756
def _iterate_slices (self ):
750
- yield self . _selection_name , self . _selected_obj
757
+ raise AbstractMethodError ( self )
751
758
752
759
def transform (self , func , * args , ** kwargs ):
753
760
raise AbstractMethodError (self )
@@ -872,6 +879,12 @@ def _cython_transform(self, how, numeric_only=True, **kwargs):
872
879
def _wrap_aggregated_output (self , output , names = None ):
873
880
raise AbstractMethodError (self )
874
881
882
+ def _wrap_transformed_output (self , output , names = None ):
883
+ raise AbstractMethodError (self )
884
+
885
+ def _wrap_applied_output (self , keys , values , not_indexed_same = False ):
886
+ raise AbstractMethodError (self )
887
+
875
888
def _cython_agg_general (self , how , alt = None , numeric_only = True , min_count = - 1 ):
876
889
output = {}
877
890
for name , obj in self ._iterate_slices ():
@@ -922,9 +935,6 @@ def _python_agg_general(self, func, *args, **kwargs):
922
935
923
936
return self ._wrap_aggregated_output (output )
924
937
925
- def _wrap_applied_output (self , * args , ** kwargs ):
926
- raise AbstractMethodError (self )
927
-
928
938
def _concat_objects (self , keys , values , not_indexed_same = False ):
929
939
from pandas .core .reshape .concat import concat
930
940
0 commit comments