@@ -1112,8 +1112,8 @@ def size(self):
1112
1112
def _add_numeric_operations (cls ):
1113
1113
""" add numeric operations to the GroupBy generically """
1114
1114
1115
- def _groupby_function (name , alias , npfunc ,
1116
- numeric_only = True , _convert = False ):
1115
+ def groupby_function (name , alias , npfunc ,
1116
+ numeric_only = True , _convert = False ):
1117
1117
1118
1118
_local_template = "Compute %(f)s of group values"
1119
1119
@@ -1125,11 +1125,13 @@ def f(self, **kwargs):
1125
1125
kwargs ['numeric_only' ] = numeric_only
1126
1126
self ._set_group_selection ()
1127
1127
try :
1128
- return self ._cython_agg_general (alias , alt = npfunc , ** kwargs )
1128
+ return self ._cython_agg_general (
1129
+ alias , alt = npfunc , ** kwargs )
1129
1130
except AssertionError as e :
1130
1131
raise SpecificationError (str (e ))
1131
1132
except Exception :
1132
- result = self .aggregate (lambda x : npfunc (x , axis = self .axis ))
1133
+ result = self .aggregate (
1134
+ lambda x : npfunc (x , axis = self .axis ))
1133
1135
if _convert :
1134
1136
result = result ._convert (datetime = True )
1135
1137
return result
@@ -1138,9 +1140,9 @@ def f(self, **kwargs):
1138
1140
1139
1141
return f
1140
1142
1141
- def _first_compat (x , axis = 0 ):
1143
+ def first_compat (x , axis = 0 ):
1142
1144
1143
- def _first (x ):
1145
+ def first (x ):
1144
1146
1145
1147
x = np .asarray (x )
1146
1148
x = x [notnull (x )]
@@ -1149,13 +1151,13 @@ def _first(x):
1149
1151
return x [0 ]
1150
1152
1151
1153
if isinstance (x , DataFrame ):
1152
- return x .apply (_first , axis = axis )
1154
+ return x .apply (first , axis = axis )
1153
1155
else :
1154
- return _first (x )
1156
+ return first (x )
1155
1157
1158
+ def last_compat (x , axis = 0 ):
1156
1159
1157
- def _last_compat (x , axis = 0 ):
1158
- def _last (x ):
1160
+ def last (x ):
1159
1161
1160
1162
x = np .asarray (x )
1161
1163
x = x [notnull (x )]
@@ -1164,18 +1166,18 @@ def _last(x):
1164
1166
return x [- 1 ]
1165
1167
1166
1168
if isinstance (x , DataFrame ):
1167
- return x .apply (_last , axis = axis )
1169
+ return x .apply (last , axis = axis )
1168
1170
else :
1169
- return _last (x )
1171
+ return last (x )
1170
1172
1171
- cls .sum = _groupby_function ('sum' , 'add' , np .sum )
1172
- cls .prod = _groupby_function ('prod' , 'prod' , np .prod )
1173
- cls .min = _groupby_function ('min' , 'min' , np .min , numeric_only = False )
1174
- cls .max = _groupby_function ('max' , 'max' , np .max , numeric_only = False )
1175
- cls .first = _groupby_function ('first' , 'first' , _first_compat ,
1176
- numeric_only = False , _convert = True )
1177
- cls .last = _groupby_function ('last' , 'last' , _last_compat , numeric_only = False ,
1178
- _convert = True )
1173
+ cls .sum = groupby_function ('sum' , 'add' , np .sum )
1174
+ cls .prod = groupby_function ('prod' , 'prod' , np .prod )
1175
+ cls .min = groupby_function ('min' , 'min' , np .min , numeric_only = False )
1176
+ cls .max = groupby_function ('max' , 'max' , np .max , numeric_only = False )
1177
+ cls .first = groupby_function ('first' , 'first' , first_compat ,
1178
+ numeric_only = False , _convert = True )
1179
+ cls .last = groupby_function ('last' , 'last' , last_compat ,
1180
+ numeric_only = False , _convert = True )
1179
1181
1180
1182
@Substitution (name = 'groupby' )
1181
1183
@Appender (_doc_template )
@@ -1587,6 +1589,7 @@ def tail(self, n=5):
1587
1589
mask = self ._cumcount_array (ascending = False ) < n
1588
1590
return self ._selected_obj [mask ]
1589
1591
1592
+
1590
1593
GroupBy ._add_numeric_operations ()
1591
1594
1592
1595
0 commit comments