@@ -786,6 +786,8 @@ def _try_cast(self, result, obj, numeric_only=False):
786786 elif is_extension_array_dtype (dtype ):
787787 # The function can return something of any type, so check
788788 # if the type is compatible with the calling EA.
789+
790+ # return the same type (Series) as our caller
789791 try :
790792 result = obj ._values ._from_sequence (result , dtype = dtype )
791793 except Exception :
@@ -1157,7 +1159,8 @@ def mean(self, *args, **kwargs):
11571159 """
11581160 nv .validate_groupby_func ('mean' , args , kwargs , ['numeric_only' ])
11591161 try :
1160- return self ._cython_agg_general ('mean' , ** kwargs )
1162+ return self ._cython_agg_general (
1163+ 'mean' , alt = lambda x , axis : Series (x ).mean (** kwargs ), ** kwargs )
11611164 except GroupByError :
11621165 raise
11631166 except Exception : # pragma: no cover
@@ -1179,7 +1182,11 @@ def median(self, **kwargs):
11791182 Median of values within each group.
11801183 """
11811184 try :
1182- return self ._cython_agg_general ('median' , ** kwargs )
1185+ return self ._cython_agg_general (
1186+ 'median' ,
1187+ alt = lambda x ,
1188+ axis : Series (x ).median (axis = axis , ** kwargs ),
1189+ ** kwargs )
11831190 except GroupByError :
11841191 raise
11851192 except Exception : # pragma: no cover
@@ -1235,7 +1242,10 @@ def var(self, ddof=1, *args, **kwargs):
12351242 nv .validate_groupby_func ('var' , args , kwargs )
12361243 if ddof == 1 :
12371244 try :
1238- return self ._cython_agg_general ('var' , ** kwargs )
1245+ return self ._cython_agg_general (
1246+ 'var' ,
1247+ alt = lambda x , axis : Series (x ).var (ddof = ddof , ** kwargs ),
1248+ ** kwargs )
12391249 except Exception :
12401250 f = lambda x : x .var (ddof = ddof , ** kwargs )
12411251 with _group_selection_context (self ):
@@ -1263,7 +1273,6 @@ def sem(self, ddof=1):
12631273 Series or DataFrame
12641274 Standard error of the mean of values within each group.
12651275 """
1266-
12671276 return self .std (ddof = ddof ) / np .sqrt (self .count ())
12681277
12691278 @Substitution (name = 'groupby' )
@@ -1290,7 +1299,7 @@ def _add_numeric_operations(cls):
12901299 """
12911300
12921301 def groupby_function (name , alias , npfunc ,
1293- numeric_only = True , _convert = False ,
1302+ numeric_only = True ,
12941303 min_count = - 1 ):
12951304
12961305 _local_template = """
@@ -1312,17 +1321,30 @@ def f(self, **kwargs):
13121321 kwargs ['min_count' ] = min_count
13131322
13141323 self ._set_group_selection ()
1324+
1325+ # try a cython aggregation if we can
13151326 try :
13161327 return self ._cython_agg_general (
13171328 alias , alt = npfunc , ** kwargs )
13181329 except AssertionError as e :
13191330 raise SpecificationError (str (e ))
13201331 except Exception :
1321- result = self .aggregate (
1322- lambda x : npfunc (x , axis = self .axis ))
1323- if _convert :
1324- result = result ._convert (datetime = True )
1325- return result
1332+ pass
1333+
1334+ # apply a non-cython aggregation
1335+ result = self .aggregate (
1336+ lambda x : npfunc (x , axis = self .axis ))
1337+
1338+ # coerce the resulting columns if we can
1339+ if isinstance (result , DataFrame ):
1340+ for col in result .columns :
1341+ result [col ] = self ._try_cast (
1342+ result [col ], self .obj [col ])
1343+ else :
1344+ result = self ._try_cast (
1345+ result , self .obj )
1346+
1347+ return result
13261348
13271349 set_function_name (f , name , cls )
13281350
0 commit comments