@@ -1130,8 +1130,8 @@ def size(self):
1130
1130
def _add_numeric_operations (cls ):
1131
1131
""" add numeric operations to the GroupBy generically """
1132
1132
1133
- def _groupby_function (name , alias , npfunc ,
1134
- numeric_only = True , _convert = False ):
1133
+ def groupby_function (name , alias , npfunc ,
1134
+ numeric_only = True , _convert = False ):
1135
1135
1136
1136
_local_template = "Compute %(f)s of group values"
1137
1137
@@ -1143,11 +1143,13 @@ def f(self, **kwargs):
1143
1143
kwargs ['numeric_only' ] = numeric_only
1144
1144
self ._set_group_selection ()
1145
1145
try :
1146
- return self ._cython_agg_general (alias , alt = npfunc , ** kwargs )
1146
+ return self ._cython_agg_general (
1147
+ alias , alt = npfunc , ** kwargs )
1147
1148
except AssertionError as e :
1148
1149
raise SpecificationError (str (e ))
1149
1150
except Exception :
1150
- result = self .aggregate (lambda x : npfunc (x , axis = self .axis ))
1151
+ result = self .aggregate (
1152
+ lambda x : npfunc (x , axis = self .axis ))
1151
1153
if _convert :
1152
1154
result = result ._convert (datetime = True )
1153
1155
return result
@@ -1156,9 +1158,9 @@ def f(self, **kwargs):
1156
1158
1157
1159
return f
1158
1160
1159
- def _first_compat (x , axis = 0 ):
1161
+ def first_compat (x , axis = 0 ):
1160
1162
1161
- def _first (x ):
1163
+ def first (x ):
1162
1164
1163
1165
x = np .asarray (x )
1164
1166
x = x [notnull (x )]
@@ -1167,13 +1169,13 @@ def _first(x):
1167
1169
return x [0 ]
1168
1170
1169
1171
if isinstance (x , DataFrame ):
1170
- return x .apply (_first , axis = axis )
1172
+ return x .apply (first , axis = axis )
1171
1173
else :
1172
- return _first (x )
1174
+ return first (x )
1173
1175
1176
+ def last_compat (x , axis = 0 ):
1174
1177
1175
- def _last_compat (x , axis = 0 ):
1176
- def _last (x ):
1178
+ def last (x ):
1177
1179
1178
1180
x = np .asarray (x )
1179
1181
x = x [notnull (x )]
@@ -1182,18 +1184,18 @@ def _last(x):
1182
1184
return x [- 1 ]
1183
1185
1184
1186
if isinstance (x , DataFrame ):
1185
- return x .apply (_last , axis = axis )
1187
+ return x .apply (last , axis = axis )
1186
1188
else :
1187
- return _last (x )
1189
+ return last (x )
1188
1190
1189
- cls .sum = _groupby_function ('sum' , 'add' , np .sum )
1190
- cls .prod = _groupby_function ('prod' , 'prod' , np .prod )
1191
- cls .min = _groupby_function ('min' , 'min' , np .min , numeric_only = False )
1192
- cls .max = _groupby_function ('max' , 'max' , np .max , numeric_only = False )
1193
- cls .first = _groupby_function ('first' , 'first' , _first_compat ,
1194
- numeric_only = False , _convert = True )
1195
- cls .last = _groupby_function ('last' , 'last' , _last_compat , numeric_only = False ,
1196
- _convert = True )
1191
+ cls .sum = groupby_function ('sum' , 'add' , np .sum )
1192
+ cls .prod = groupby_function ('prod' , 'prod' , np .prod )
1193
+ cls .min = groupby_function ('min' , 'min' , np .min , numeric_only = False )
1194
+ cls .max = groupby_function ('max' , 'max' , np .max , numeric_only = False )
1195
+ cls .first = groupby_function ('first' , 'first' , first_compat ,
1196
+ numeric_only = False , _convert = True )
1197
+ cls .last = groupby_function ('last' , 'last' , last_compat ,
1198
+ numeric_only = False , _convert = True )
1197
1199
1198
1200
@Substitution (name = 'groupby' )
1199
1201
@Appender (_doc_template )
@@ -1605,6 +1607,7 @@ def tail(self, n=5):
1605
1607
mask = self ._cumcount_array (ascending = False ) < n
1606
1608
return self ._selected_obj [mask ]
1607
1609
1610
+
1608
1611
GroupBy ._add_numeric_operations ()
1609
1612
1610
1613
0 commit comments