@@ -1249,26 +1249,30 @@ def group_min(groupby_t[:, ::1] out,
1249
1249
1250
1250
@ cython.boundscheck (False )
1251
1251
@ cython.wraparound (False )
1252
- def group_cummin (groupby_t[:, ::1] out ,
1253
- ndarray[groupby_t , ndim = 2 ] values,
1254
- const int64_t[:] labels ,
1255
- int ngroups ,
1256
- bint is_datetimelike ):
1252
+ def group_cummin_max (groupby_t[:, ::1] out ,
1253
+ ndarray[groupby_t , ndim = 2 ] values,
1254
+ const int64_t[:] labels ,
1255
+ int ngroups ,
1256
+ bint is_datetimelike ,
1257
+ bint compute_max ):
1257
1258
"""
1258
- Cumulative minimum of columns of `values`, in row groups `labels`.
1259
+ Cumulative minimum/maximum of columns of `values`, in row groups `labels`.
1259
1260
1260
1261
Parameters
1261
1262
----------
1262
1263
out : array
1263
- Array to store cummin in.
1264
+ Array to store cummin/max in.
1264
1265
values : array
1265
- Values to take cummin of.
1266
+ Values to take cummin/max of.
1266
1267
labels : int64 array
1267
1268
Labels to group by.
1268
1269
ngroups : int
1269
1270
Number of groups, larger than all entries of `labels`.
1270
1271
is_datetimelike : bool
1271
1272
True if `values` contains datetime-like entries.
1273
+ compute_max : bool
1274
+ True if cumulative maximum should be computed, False
1275
+ if cumulative minimum should be computed
1272
1276
1273
1277
Notes
1274
1278
-----
@@ -1283,11 +1287,11 @@ def group_cummin(groupby_t[:, ::1] out,
1283
1287
N, K = (< object > values).shape
1284
1288
accum = np.empty((ngroups, K), dtype = np.asarray(values).dtype)
1285
1289
if groupby_t is int64_t:
1286
- accum[:] = _int64_max
1290
+ accum[:] = - _int64_max if compute_max else _int64_max
1287
1291
elif groupby_t is uint64_t:
1288
- accum[:] = np.iinfo(np.uint64).max
1292
+ accum[:] = 0 if compute_max else np.iinfo(np.uint64).max
1289
1293
else :
1290
- accum[:] = np.inf
1294
+ accum[:] = - np.inf if compute_max else np.inf
1291
1295
1292
1296
with nogil:
1293
1297
for i in range (N):
@@ -1302,66 +1306,32 @@ def group_cummin(groupby_t[:, ::1] out,
1302
1306
out[i, j] = val
1303
1307
else :
1304
1308
mval = accum[lab, j]
1305
- if val < mval:
1306
- accum[lab, j] = mval = val
1309
+ if compute_max:
1310
+ if val > mval:
1311
+ accum[lab, j] = mval = val
1312
+ else :
1313
+ if val < mval:
1314
+ accum[lab, j] = mval = val
1307
1315
out[i, j] = mval
1308
1316
1309
1317
1310
1318
@ cython.boundscheck (False )
1311
1319
@ cython.wraparound (False )
1312
- def group_cummax (groupby_t[:, ::1] out ,
1320
+ def group_cummin (groupby_t[:, ::1] out ,
1313
1321
ndarray[groupby_t , ndim = 2 ] values,
1314
1322
const int64_t[:] labels ,
1315
1323
int ngroups ,
1316
1324
bint is_datetimelike ):
1317
- """
1318
- Cumulative maximum of columns of ` values`, in row groups `labels`.
1325
+ """ See group_cummin_max.__doc__ """
1326
+ group_cummin_max(out, values, labels, ngroups, is_datetimelike, compute_max = False )
1319
1327
1320
- Parameters
1321
- ----------
1322
- out : array
1323
- Array to store cummax in.
1324
- values : array
1325
- Values to take cummax of.
1326
- labels : int64 array
1327
- Labels to group by.
1328
- ngroups : int
1329
- Number of groups, larger than all entries of `labels`.
1330
- is_datetimelike : bool
1331
- True if `values` contains datetime-like entries.
1332
1328
1333
- Notes
1334
- -----
1335
- This method modifies the `out` parameter, rather than returning an object.
1336
- """
1337
- cdef:
1338
- Py_ssize_t i, j, N, K, size
1339
- groupby_t val, mval
1340
- ndarray[groupby_t, ndim= 2 ] accum
1341
- int64_t lab
1342
-
1343
- N, K = (< object > values).shape
1344
- accum = np.empty((ngroups, K), dtype = np.asarray(values).dtype)
1345
- if groupby_t is int64_t:
1346
- accum[:] = - _int64_max
1347
- elif groupby_t is uint64_t:
1348
- accum[:] = 0
1349
- else :
1350
- accum[:] = - np.inf
1351
-
1352
- with nogil:
1353
- for i in range (N):
1354
- lab = labels[i]
1355
-
1356
- if lab < 0 :
1357
- continue
1358
- for j in range (K):
1359
- val = values[i, j]
1360
-
1361
- if _treat_as_na(val, is_datetimelike):
1362
- out[i, j] = val
1363
- else :
1364
- mval = accum[lab, j]
1365
- if val > mval:
1366
- accum[lab, j] = mval = val
1367
- out[i, j] = mval
1329
+ @ cython.boundscheck (False )
1330
+ @ cython.wraparound (False )
1331
+ def group_cummax (groupby_t[:, ::1] out ,
1332
+ ndarray[groupby_t , ndim = 2 ] values,
1333
+ const int64_t[:] labels ,
1334
+ int ngroups ,
1335
+ bint is_datetimelike ):
1336
+ """ See group_cummin_max.__doc__"""
1337
+ group_cummin_max(out, values, labels, ngroups, is_datetimelike, compute_max = True )
0 commit comments