@@ -31,9 +31,9 @@ dtypes = [('Complex128', 'complex128', 'complex128',
31
31
@cython.wraparound(False)
32
32
@cython.boundscheck(False)
33
33
{{if dtype == 'object'}}
34
- cdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna):
34
+ cdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna, const uint8_t[:] mask=None ):
35
35
{{else}}
36
- cdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
36
+ cdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna, const uint8_t[:] mask=None ):
37
37
{{endif}}
38
38
cdef:
39
39
Py_ssize_t i = 0
@@ -46,6 +46,11 @@ cdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
46
46
{{c_type}} val
47
47
48
48
int ret = 0
49
+ bint uses_mask = mask is not None
50
+ bint isna_entry = False
51
+
52
+ if uses_mask and not dropna:
53
+ raise NotImplementedError("uses_mask not implemented with dropna=False")
49
54
50
55
# we track the order in which keys are first seen (GH39009),
51
56
# khash-map isn't insertion-ordered, thus:
@@ -56,6 +61,9 @@ cdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
56
61
table = kh_init_{{ttype}}()
57
62
58
63
{{if dtype == 'object'}}
64
+ if uses_mask:
65
+ raise NotImplementedError("uses_mask not implemented with object dtype")
66
+
59
67
kh_resize_{{ttype}}(table, n // 10)
60
68
61
69
for i in range(n):
@@ -74,7 +82,13 @@ cdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
74
82
for i in range(n):
75
83
val = {{to_c_type}}(values[i])
76
84
77
- if not is_nan_{{c_type}}(val) or not dropna:
85
+ if dropna:
86
+ if uses_mask:
87
+ isna_entry = mask[i]
88
+ else:
89
+ isna_entry = is_nan_{{c_type}}(val)
90
+
91
+ if not dropna or not isna_entry:
78
92
k = kh_get_{{ttype}}(table, val)
79
93
if k != table.n_buckets:
80
94
table.vals[k] += 1
@@ -251,37 +265,37 @@ ctypedef fused htfunc_t:
251
265
complex64_t
252
266
253
267
254
- cpdef value_count(ndarray[htfunc_t] values, bint dropna):
268
+ cpdef value_count(ndarray[htfunc_t] values, bint dropna, const uint8_t[:] mask=None ):
255
269
if htfunc_t is object:
256
- return value_count_object(values, dropna)
270
+ return value_count_object(values, dropna, mask=mask )
257
271
258
272
elif htfunc_t is int8_t:
259
- return value_count_int8(values, dropna)
273
+ return value_count_int8(values, dropna, mask=mask )
260
274
elif htfunc_t is int16_t:
261
- return value_count_int16(values, dropna)
275
+ return value_count_int16(values, dropna, mask=mask )
262
276
elif htfunc_t is int32_t:
263
- return value_count_int32(values, dropna)
277
+ return value_count_int32(values, dropna, mask=mask )
264
278
elif htfunc_t is int64_t:
265
- return value_count_int64(values, dropna)
279
+ return value_count_int64(values, dropna, mask=mask )
266
280
267
281
elif htfunc_t is uint8_t:
268
- return value_count_uint8(values, dropna)
282
+ return value_count_uint8(values, dropna, mask=mask )
269
283
elif htfunc_t is uint16_t:
270
- return value_count_uint16(values, dropna)
284
+ return value_count_uint16(values, dropna, mask=mask )
271
285
elif htfunc_t is uint32_t:
272
- return value_count_uint32(values, dropna)
286
+ return value_count_uint32(values, dropna, mask=mask )
273
287
elif htfunc_t is uint64_t:
274
- return value_count_uint64(values, dropna)
288
+ return value_count_uint64(values, dropna, mask=mask )
275
289
276
290
elif htfunc_t is float64_t:
277
- return value_count_float64(values, dropna)
291
+ return value_count_float64(values, dropna, mask=mask )
278
292
elif htfunc_t is float32_t:
279
- return value_count_float32(values, dropna)
293
+ return value_count_float32(values, dropna, mask=mask )
280
294
281
295
elif htfunc_t is complex128_t:
282
- return value_count_complex128(values, dropna)
296
+ return value_count_complex128(values, dropna, mask=mask )
283
297
elif htfunc_t is complex64_t:
284
- return value_count_complex64(values, dropna)
298
+ return value_count_complex64(values, dropna, mask=mask )
285
299
286
300
else:
287
301
raise TypeError(values.dtype)
@@ -361,7 +375,7 @@ cpdef ismember(ndarray[htfunc_t] arr, ndarray[htfunc_t] values):
361
375
362
376
@cython.wraparound(False)
363
377
@cython.boundscheck(False)
364
- def mode(ndarray[htfunc_t] values, bint dropna):
378
+ def mode(ndarray[htfunc_t] values, bint dropna, const uint8_t[:] mask=None ):
365
379
# TODO(cython3): use const htfunct_t[:]
366
380
367
381
cdef:
@@ -372,7 +386,7 @@ def mode(ndarray[htfunc_t] values, bint dropna):
372
386
int64_t count, max_count = -1
373
387
Py_ssize_t nkeys, k, j = 0
374
388
375
- keys, counts = value_count(values, dropna)
389
+ keys, counts = value_count(values, dropna, mask=mask )
376
390
nkeys = len(keys)
377
391
378
392
modes = np.empty(nkeys, dtype=values.dtype)
0 commit comments