Skip to content

Commit 88903b9

Browse files
committed
docs: adds comment to fix using ndarray and fixes indenting
1 parent ff31253 commit 88903b9

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+46-41
Original file line numberDiff line numberDiff line change
@@ -258,44 +258,44 @@ dtypes = [('Float64', 'float64', 'val != val', True),
258258
def get_dispatch(dtypes):
259259
for (name, dtype, null_condition, float_group) in dtypes:
260260
unique_template = """\
261-
cdef:
262-
Py_ssize_t i, n = len(values)
263-
int ret = 0
264-
{dtype}_t val
265-
khiter_t k
266-
bint seen_na = 0
267-
{name}Vector uniques = {name}Vector()
268-
{name}VectorData *ud
269-
270-
ud = uniques.data
271-
272-
with nogil:
273-
for i in range(n):
274-
val = values[i]
275-
IF {float_group}:
276-
if val == val:
277-
k = kh_get_{dtype}(self.table, val)
278-
if k == self.table.n_buckets:
279-
kh_put_{dtype}(self.table, val, &ret)
280-
if needs_resize(ud):
281-
with gil:
282-
uniques.resize()
283-
append_data_{dtype}(ud, val)
284-
elif not seen_na:
285-
seen_na = 1
286-
if needs_resize(ud):
287-
with gil:
288-
uniques.resize()
289-
append_data_{dtype}(ud, NAN)
290-
ELSE:
291-
k = kh_get_{dtype}(self.table, val)
292-
if k == self.table.n_buckets:
293-
kh_put_{dtype}(self.table, val, &ret)
294-
if needs_resize(ud):
295-
with gil:
296-
uniques.resize()
297-
append_data_{dtype}(ud, val)
298-
return uniques.to_array()
261+
cdef:
262+
Py_ssize_t i, n = len(values)
263+
int ret = 0
264+
{dtype}_t val
265+
khiter_t k
266+
bint seen_na = 0
267+
{name}Vector uniques = {name}Vector()
268+
{name}VectorData *ud
269+
270+
ud = uniques.data
271+
272+
with nogil:
273+
for i in range(n):
274+
val = values[i]
275+
IF {float_group}:
276+
if val == val:
277+
k = kh_get_{dtype}(self.table, val)
278+
if k == self.table.n_buckets:
279+
kh_put_{dtype}(self.table, val, &ret)
280+
if needs_resize(ud):
281+
with gil:
282+
uniques.resize()
283+
append_data_{dtype}(ud, val)
284+
elif not seen_na:
285+
seen_na = 1
286+
if needs_resize(ud):
287+
with gil:
288+
uniques.resize()
289+
append_data_{dtype}(ud, NAN)
290+
ELSE:
291+
k = kh_get_{dtype}(self.table, val)
292+
if k == self.table.n_buckets:
293+
kh_put_{dtype}(self.table, val, &ret)
294+
if needs_resize(ud):
295+
with gil:
296+
uniques.resize()
297+
append_data_{dtype}(ud, val)
298+
return uniques.to_array()
299299
"""
300300

301301
unique_template = unique_template.format(name=name, dtype=dtype, null_condition=null_condition, float_group=float_group)
@@ -497,9 +497,14 @@ cdef class {{name}}HashTable(HashTable):
497497

498498
@cython.boundscheck(False)
499499
def unique(self, ndarray[{{dtype}}_t, ndim=1] values):
500-
if values.flags.writeable:
501-
return self.unique_memview(values)
502-
500+
if values.flags.writeable:
501+
# If the value is writeable (mutable) then use memview
502+
return self.unique_memview(values)
503+
504+
# We cannot use the memoryview version on readonly-buffers due to
505+
# a limitation of Cython's typed memoryviews. Instead we can use
506+
# the slightly slower Cython ndarray type directly.
507+
# see https://github.com/cython/cython/issues/1605
503508
{{unique_template}}
504509

505510
@cython.boundscheck(False)

0 commit comments

Comments
 (0)