@@ -312,58 +312,23 @@ def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
312
312
313
313
@ cython.boundscheck (False )
314
314
@ cython.wraparound (False )
315
- def group_any (ndarray[uint8_t] out ,
316
- ndarray[int64_t] labels ,
317
- ndarray[uint8_t] values ,
318
- ndarray[uint8_t] mask ,
319
- bint skipna ):
320
- """ Aggregated boolean values to show if any group element is truthful
315
+ def group_any_all (ndarray[uint8_t] out ,
316
+ ndarray[int64_t] labels ,
317
+ ndarray[uint8_t] values ,
318
+ ndarray[uint8_t] mask ,
319
+ object val_test ,
320
+ bint skipna ):
321
+ """ Aggregated boolean values to show truthfulness of group elements
321
322
322
323
Parameters
323
324
----------
324
325
out : array of values which this method will write its results to
325
- labels : array containing unique label for each group, with its ordering
326
- matching up to the corresponding record in `values`
327
- values : array containing the truth value of each element
328
- mask : array indicating whether a value is na or not
329
- skipna : boolean
330
- Flag to ignore nan values during truth testing
331
-
332
- Notes
333
- -----
334
- This method modifies the `out` parameter rather than returning an object.
335
- The returned values will either be 0 or 1 (False or True, respectively).
336
- """
337
- cdef:
338
- Py_ssize_t i, N= len (labels)
339
- int64_t lab
340
-
341
- with nogil:
342
- for i in range (N):
343
- lab = labels[i]
344
- if lab < 0 or (skipna and mask[i]):
345
- continue
346
-
347
- if values[i]:
348
- out[lab] = 1
349
-
350
-
351
- @ cython.boundscheck (False )
352
- @ cython.wraparound (False )
353
- def group_all (ndarray[uint8_t] out ,
354
- ndarray[int64_t] labels ,
355
- ndarray[uint8_t] values ,
356
- ndarray[uint8_t] mask ,
357
- bint skipna ):
358
- """ Aggregated boolean values to show if all group elements are truthful
359
-
360
- Parameters
361
- ----------
362
- out : array of values which this method will write its results to
363
- labels : array containing unique label for each group, with its ordering
364
- matching up to the corresponding record in `values`
326
+ labels : array containing unique label for each group, with its
327
+ ordering matching up to the corresponding record in `values`
365
328
values : array containing the truth value of each element
366
329
mask : array indicating whether a value is na or not
330
+ val_test : str {'any', 'all'}
331
+ String object dictating whether to use any or all truth testing
367
332
skipna : boolean
368
333
Flag to ignore nan values during truth testing
369
334
@@ -374,23 +339,31 @@ def group_all(ndarray[uint8_t] out,
374
339
"""
375
340
cdef:
376
341
Py_ssize_t i, N= len (labels)
377
- int64_t lab
378
- ndarray[int64_t] bool_mask
379
- ndarray[uint8_t] isna_mask
342
+ int64_t lab, flag_val
343
+
344
+ if val_test == ' all' :
345
+ # Because the 'all' value of an empty iterable in Python is True we can
346
+ # start with an array full of ones and set to zero when a False value
347
+ # is encountered
348
+ flag_val = 0
349
+ elif val_test == ' any' :
350
+ # Because the 'any' value of an empty iterable in Python is False we
351
+ # can start with an array full of zeros and set to one only if any
352
+ # value encountered is True
353
+ flag_val = 1
354
+ else :
355
+ raise ValueError (" 'bool_func' must be either 'any' or 'all'!" )
380
356
381
- # Because the 'all' value of an empty iterable in Python is True we can
382
- # start with an array full of ones and set to zero when a False value is
383
- # encountered
384
- out.fill(1 )
357
+ out.fill(1 - flag_val)
385
358
386
359
with nogil:
387
360
for i in range (N):
388
361
lab = labels[i]
389
362
if lab < 0 or (skipna and mask[i]):
390
363
continue
391
364
392
- if not values[i]:
393
- out[lab] = 0
365
+ if values[i] == flag_val :
366
+ out[lab] = flag_val
394
367
395
368
396
369
# generated from template
0 commit comments