Skip to content

Commit 6e54f2e

Browse files
committed
keep original order also for null-objects for dtype=np.object
1 parent 58fbc7f commit 6e54f2e

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

pandas/_libs/hashtable_func_helper.pxi.in

+7-12
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ cdef build_count_table_{{dtype}}(const {{dtype}}_t[:] values,
7777
@cython.wraparound(False)
7878
@cython.boundscheck(False)
7979
{{if dtype == 'object'}}
80-
cpdef stable_value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna):
80+
cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna, navalue=np.NaN):
8181
{{else}}
82-
cpdef stable_value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
82+
cpdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
8383
{{endif}}
8484
cdef:
8585
Py_ssize_t i = 0
@@ -90,6 +90,7 @@ cpdef stable_value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
9090

9191
# Don't use Py_ssize_t, since table.n_buckets is unsigned
9292
khiter_t k
93+
bint is_null
9394

9495
{{c_type}} val
9596

@@ -108,7 +109,10 @@ cpdef stable_value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
108109

109110
for i in range(n):
110111
val = values[i]
111-
if not checknull(val) or not dropna:
112+
is_null = checknull(val)
113+
if is_null:
114+
val = navalue
115+
if not is_null or not dropna:
112116
k = kh_get_{{ttype}}(table, <PyObject*>val)
113117
if k != table.n_buckets:
114118
unique_key_index = table.vals[k]
@@ -143,15 +147,6 @@ cpdef stable_value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
143147
return result_keys.to_array(), result_counts.to_array()
144148

145149

146-
{{if dtype == 'object'}}
147-
cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna):
148-
return stable_value_count_{{dtype}}(values, 1)
149-
{{else}}
150-
cpdef value_count_{{dtype}}(const {{dtype}}_t[:] values, bint dropna):
151-
return stable_value_count_{{dtype}}(values, dropna)
152-
{{endif}}
153-
154-
155150
@cython.wraparound(False)
156151
@cython.boundscheck(False)
157152
{{if dtype == 'object'}}

pandas/core/algorithms.py

-6
Original file line numberDiff line numberDiff line change
@@ -874,12 +874,6 @@ def value_counts_arraylike(values, dropna: bool):
874874
f = getattr(htable, f"value_count_{ndtype}")
875875
keys, counts = f(values, dropna)
876876

877-
mask = isna(values)
878-
if not dropna and mask.any():
879-
if not isna(keys).any():
880-
keys = np.insert(keys, 0, np.NaN)
881-
counts = np.insert(counts, 0, mask.sum())
882-
883877
keys = _reconstruct_data(keys, original.dtype, original)
884878

885879
return keys, counts

0 commit comments

Comments
 (0)