Skip to content

Commit 2d5d0f9

Browse files
committed
making count_values and mode accept constand input as well
1 parent 3db63ef commit 2d5d0f9

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

pandas/_libs/hashtable_func_helper.pxi.in

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dtypes = [('complex128', 'complex128', 'khcomplex128_t', True),
3232
cdef build_count_table_{{dtype}}(ndarray[{{dtype}}] values,
3333
kh_{{ttype}}_t *table, bint dropna):
3434
{{else}}
35-
cdef build_count_table_{{dtype}}({{c_type}}[:] values,
35+
cdef build_count_table_{{dtype}}(const {{dtype}}_t[:] values,
3636
kh_{{ttype}}_t *table, bint dropna):
3737
{{endif}}
3838
cdef:
@@ -47,8 +47,11 @@ cdef build_count_table_{{dtype}}({{c_type}}[:] values,
4747
kh_resize_{{ttype}}(table, n // 10)
4848

4949
for i in range(n):
50+
{{if complex_group}}
51+
val = to_{{c_type}}(values[i])
52+
{{else}}
5053
val = values[i]
51-
54+
{{endif}}
5255
if not checknull(val) or not dropna:
5356
k = kh_get_{{ttype}}(table, <PyObject*>val)
5457
if k != table.n_buckets:
@@ -61,7 +64,11 @@ cdef build_count_table_{{dtype}}({{c_type}}[:] values,
6164
kh_resize_{{ttype}}(table, n)
6265

6366
for i in range(n):
67+
{{if complex_group}}
68+
val = to_{{c_type}}(values[i])
69+
{{else}}
6470
val = values[i]
71+
{{endif}}
6572

6673
{{if dtype == 'float64' or dtype == 'float32'}}
6774
if val == val or not dropna:
@@ -84,7 +91,7 @@ cdef build_count_table_{{dtype}}({{c_type}}[:] values,
8491
{{if dtype == 'object'}}
8592
cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna):
8693
{{else}}
87-
cpdef value_count_{{dtype}}({{c_type}}[:] values, bint dropna):
94+
cpdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
8895
{{endif}}
8996
cdef:
9097
Py_ssize_t i = 0
@@ -338,7 +345,7 @@ def mode_{{dtype}}(ndarray[{{ctype}}] values, bint dropna):
338345
{{else}}
339346

340347

341-
def mode_{{dtype}}({{ctype}}[:] values, bint dropna):
348+
def mode_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
342349
{{endif}}
343350
cdef:
344351
int count, max_count = 1

pandas/tests/libs/test_hashtable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def test_value_count(self, dtype, type_suffix):
251251
value_count = get_ht_function("value_count", type_suffix)
252252
expected = (np.arange(N) + N).astype(dtype)
253253
values = np.repeat(expected, 5)
254+
values.flags.writeable = False
254255
keys, counts = value_count(values, False)
255256
tm.assert_numpy_array_equal(np.sort(keys), expected)
256257
assert np.all(counts == 5)
@@ -293,6 +294,7 @@ def test_mode(self, dtype, type_suffix):
293294
mode = get_ht_function("mode", type_suffix)
294295
values = np.repeat(np.arange(N).astype(dtype), 5)
295296
values[0] = 42
297+
values.flags.writeable = False
296298
result = mode(values, False)
297299
assert result == 42
298300

0 commit comments

Comments
 (0)