-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: making value_counts stable/keeping original ordering #39009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
18050cb
bfa7e57
773774b
d96118f
ca68f30
7fd9153
9962c3a
679044b
8097e5b
95f2247
d480b7c
90d0c52
d85b1aa
230215f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,26 +6,26 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in | |
|
||
{{py: | ||
|
||
# dtype, ttype, c_type, to_c_type, to_dtype | ||
dtypes = [('complex128', 'complex128', 'khcomplex128_t', | ||
'to_khcomplex128_t', 'to_complex128'), | ||
('complex64', 'complex64', 'khcomplex64_t', | ||
'to_khcomplex64_t', 'to_complex64'), | ||
('float64', 'float64', 'float64_t', '', ''), | ||
('float32', 'float32', 'float32_t', '', ''), | ||
('uint64', 'uint64', 'uint64_t', '', ''), | ||
('uint32', 'uint32', 'uint32_t', '', ''), | ||
('uint16', 'uint16', 'uint16_t', '', ''), | ||
('uint8', 'uint8', 'uint8_t', '', ''), | ||
('object', 'pymap', 'object', '', ''), | ||
('int64', 'int64', 'int64_t', '', ''), | ||
('int32', 'int32', 'int32_t', '', ''), | ||
('int16', 'int16', 'int16_t', '', ''), | ||
('int8', 'int8', 'int8_t', '', '')] | ||
# name, dtype, ttype, c_type, to_c_type | ||
dtypes = [('Complex128', 'complex128', 'complex128', | ||
'khcomplex128_t', 'to_khcomplex128_t'), | ||
('Complex64', 'complex64', 'complex64', | ||
'khcomplex64_t', 'to_khcomplex64_t'), | ||
('Float64', 'float64', 'float64', 'float64_t', ''), | ||
('Float32', 'float32', 'float32', 'float32_t', ''), | ||
('UInt64', 'uint64', 'uint64', 'uint64_t', ''), | ||
('UInt32', 'uint32', 'uint32', 'uint32_t', ''), | ||
('UInt16', 'uint16', 'uint16', 'uint16_t', ''), | ||
('UInt8', 'uint8', 'uint8', 'uint8_t', ''), | ||
('Object', 'object', 'pymap', 'object', ''), | ||
('Int64', 'int64', 'int64', 'int64_t', ''), | ||
('Int32', 'int32', 'int32', 'int32_t', ''), | ||
('Int16', 'int16', 'int16', 'int16_t', ''), | ||
('Int8', 'int8', 'int8', 'int8_t', '')] | ||
|
||
}} | ||
|
||
{{for dtype, ttype, c_type, to_c_type, to_dtype in dtypes}} | ||
{{for name, dtype, ttype, c_type, to_c_type in dtypes}} | ||
|
||
|
||
@cython.wraparound(False) | ||
|
@@ -77,54 +77,77 @@ cdef build_count_table_{{dtype}}(const {{dtype}}_t[:] values, | |
@cython.wraparound(False) | ||
@cython.boundscheck(False) | ||
{{if dtype == 'object'}} | ||
cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna): | ||
cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna, navalue=np.NaN): | ||
{{else}} | ||
cpdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna): | ||
{{endif}} | ||
cdef: | ||
Py_ssize_t i = 0 | ||
Py_ssize_t n = len(values) | ||
size_t unique_key_index = 0 | ||
size_t unique_key_count = 0 | ||
kh_{{ttype}}_t *table | ||
|
||
{{if dtype != 'object'}} | ||
{{dtype}}_t[:] result_keys | ||
int64_t[:] result_counts | ||
{{endif}} | ||
|
||
# Don't use Py_ssize_t, since table.n_buckets is unsigned | ||
khiter_t k | ||
bint is_null | ||
|
||
{{c_type}} val | ||
|
||
int ret = 0 | ||
|
||
# we track the order in which keys are first seen (GH39009), | ||
# khash-map isn't insertion-ordered, thus: | ||
# table maps key to index_of_appearence | ||
# result_keys maps index_of_appearence to key | ||
# result_counts maps index_of_appearence to number of elements | ||
|
||
result_keys = {{name}}Vector() | ||
result_counts = Int64Vector() | ||
table = kh_init_{{ttype}}() | ||
|
||
{{if dtype == 'object'}} | ||
build_count_table_{{dtype}}(values, table, 1) | ||
kh_resize_{{ttype}}(table, n // 10) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for i in range(n): | ||
val = values[i] | ||
is_null = checknull(val) | ||
if not is_null or not dropna: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be elif There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how it could work with elif (but I'm always struggeling with logic). I have rearanged the if and hope it is clearer now: 230215f |
||
# all nas become the same representative: | ||
if is_null: | ||
val = navalue | ||
k = kh_get_{{ttype}}(table, <PyObject*>val) | ||
if k != table.n_buckets: | ||
unique_key_index = table.vals[k] | ||
result_counts.data.data[unique_key_index] += 1 | ||
else: | ||
k = kh_put_{{ttype}}(table, <PyObject*>val, &ret) | ||
table.vals[k] = unique_key_count | ||
result_keys.append(val) | ||
result_counts.append(1) | ||
unique_key_count+=1 | ||
{{else}} | ||
build_count_table_{{dtype}}(values, table, dropna) | ||
{{endif}} | ||
kh_resize_{{ttype}}(table, n) | ||
|
||
result_keys = np.empty(table.n_occupied, '{{dtype}}') | ||
result_counts = np.zeros(table.n_occupied, dtype=np.int64) | ||
for i in range(n): | ||
val = {{to_c_type}}(values[i]) | ||
|
||
{{if dtype == 'object'}} | ||
for k in range(table.n_buckets): | ||
if kh_exist_{{ttype}}(table, k): | ||
result_keys[i] = <{{dtype}}>table.keys[k] | ||
result_counts[i] = table.vals[k] | ||
i += 1 | ||
{{else}} | ||
with nogil: | ||
for k in range(table.n_buckets): | ||
if kh_exist_{{ttype}}(table, k): | ||
result_keys[i] = {{to_dtype}}(table.keys[k]) | ||
result_counts[i] = table.vals[k] | ||
i += 1 | ||
if not is_nan_{{c_type}}(val) or not dropna: | ||
k = kh_get_{{ttype}}(table, val) | ||
if k != table.n_buckets: | ||
unique_key_index = table.vals[k] | ||
result_counts.data.data[unique_key_index] += 1 | ||
else: | ||
k = kh_put_{{ttype}}(table, val, &ret) | ||
table.vals[k] = unique_key_count | ||
result_keys.append(val) | ||
result_counts.append(1) | ||
unique_key_count+=1 | ||
{{endif}} | ||
|
||
kh_destroy_{{ttype}}(table) | ||
|
||
{{if dtype == 'object'}} | ||
return result_keys, result_counts | ||
{{else}} | ||
return np.asarray(result_keys), np.asarray(result_counts) | ||
{{endif}} | ||
return result_keys.to_array(), result_counts.to_array() | ||
|
||
|
||
@cython.wraparound(False) | ||
|
Uh oh!
There was an error while loading. Please reload this page.