Skip to content

Commit 3db63ef

Browse files
committed
const memoryviews of struct dtypes will work only with Cython>=0.29.22, so making a workaround
1 parent a915893 commit 3db63ef

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ cdef class {{name}}HashTable(HashTable):
423423
raise KeyError(key)
424424

425425
@cython.boundscheck(False)
426-
def map(self, const {{c_type}}[:] keys, const int64_t[:] values):
426+
def map(self, const {{dtype}}_t[:] keys, const int64_t[:] values):
427427
cdef:
428428
Py_ssize_t i, n = len(values)
429429
int ret = 0
@@ -432,12 +432,16 @@ cdef class {{name}}HashTable(HashTable):
432432

433433
with nogil:
434434
for i in range(n):
435+
{{if complex_group}}
436+
key = to_{{c_type}}(keys[i])
437+
{{else}}
435438
key = keys[i]
439+
{{endif}}
436440
k = kh_put_{{dtype}}(self.table, key, &ret)
437441
self.table.vals[k] = <Py_ssize_t>values[i]
438442

439443
@cython.boundscheck(False)
440-
def map_locations(self, const {{c_type}}[:] values):
444+
def map_locations(self, const {{dtype}}_t[:] values):
441445
cdef:
442446
Py_ssize_t i, n = len(values)
443447
int ret = 0
@@ -446,12 +450,16 @@ cdef class {{name}}HashTable(HashTable):
446450

447451
with nogil:
448452
for i in range(n):
453+
{{if complex_group}}
454+
val= to_{{c_type}}(values[i])
455+
{{else}}
449456
val = values[i]
457+
{{endif}}
450458
k = kh_put_{{dtype}}(self.table, val, &ret)
451459
self.table.vals[k] = i
452460

453461
@cython.boundscheck(False)
454-
def lookup(self, const {{c_type}}[:] values):
462+
def lookup(self, const {{dtype}}_t[:] values):
455463
cdef:
456464
Py_ssize_t i, n = len(values)
457465
int ret = 0
@@ -461,7 +469,11 @@ cdef class {{name}}HashTable(HashTable):
461469

462470
with nogil:
463471
for i in range(n):
472+
{{if complex_group}}
473+
val = to_{{c_type}}(values[i])
474+
{{else}}
464475
val = values[i]
476+
{{endif}}
465477
k = kh_get_{{dtype}}(self.table, val)
466478
if k != self.table.n_buckets:
467479
locs[i] = self.table.vals[k]
@@ -472,7 +484,7 @@ cdef class {{name}}HashTable(HashTable):
472484

473485
@cython.boundscheck(False)
474486
@cython.wraparound(False)
475-
def _unique(self, const {{c_type}}[:] values, {{name}}Vector uniques,
487+
def _unique(self, const {{dtype}}_t[:] values, {{name}}Vector uniques,
476488
Py_ssize_t count_prior=0, Py_ssize_t na_sentinel=-1,
477489
object na_value=None, bint ignore_na=False,
478490
object mask=None, bint return_inverse=False):
@@ -552,7 +564,11 @@ cdef class {{name}}HashTable(HashTable):
552564

553565
with nogil:
554566
for i in range(n):
567+
{{if complex_group}}
568+
val = to_{{c_type}}(values[i])
569+
{{else}}
555570
val = values[i]
571+
{{endif}}
556572

557573
if ignore_na and use_mask:
558574
if mask_values[i]:
@@ -602,7 +618,7 @@ cdef class {{name}}HashTable(HashTable):
602618
return uniques.to_array(), np.asarray(labels)
603619
return uniques.to_array()
604620

605-
def unique(self, const {{c_type}}[:] values, bint return_inverse=False):
621+
def unique(self, const {{dtype}}_t[:] values, bint return_inverse=False):
606622
"""
607623
Calculate unique values and labels (no sorting!)
608624

@@ -625,7 +641,7 @@ cdef class {{name}}HashTable(HashTable):
625641
return self._unique(values, uniques, ignore_na=False,
626642
return_inverse=return_inverse)
627643

628-
def factorize(self, const {{c_type}}[:] values, Py_ssize_t na_sentinel=-1,
644+
def factorize(self, const {{dtype}}_t[:] values, Py_ssize_t na_sentinel=-1,
629645
object na_value=None, object mask=None):
630646
"""
631647
Calculate unique values and labels (no sorting!)
@@ -670,7 +686,7 @@ cdef class {{name}}HashTable(HashTable):
670686
return labels
671687

672688
@cython.boundscheck(False)
673-
def get_labels_groupby(self, const {{c_type}}[:] values):
689+
def get_labels_groupby(self, const {{dtype}}_t[:] values):
674690
cdef:
675691
Py_ssize_t i, n = len(values)
676692
intp_t[:] labels
@@ -686,7 +702,11 @@ cdef class {{name}}HashTable(HashTable):
686702

687703
with nogil:
688704
for i in range(n):
705+
{{if complex_group}}
706+
val = to_{{c_type}}(values[i])
707+
{{else}}
689708
val = values[i]
709+
{{endif}}
690710

691711
# specific for groupby
692712
{{if dtype == 'complex64' or dtype== 'complex128'}}

pandas/_libs/hashtable_func_helper.pxi.in

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ cpdef value_count_{{dtype}}({{c_type}}[:] values, bint dropna):
141141
{{if dtype == 'object'}}
142142
def duplicated_{{dtype}}(ndarray[{{dtype}}] values, object keep='first'):
143143
{{else}}
144-
def duplicated_{{dtype}}(const {{c_type}}[:] values, object keep='first'):
144+
def duplicated_{{dtype}}(const {{dtype}}_t[:] values, object keep='first'):
145145
{{endif}}
146146
cdef:
147147
int ret = 0
@@ -168,7 +168,12 @@ def duplicated_{{dtype}}(const {{c_type}}[:] values, object keep='first'):
168168
with nogil:
169169
for i in range(n - 1, -1, -1):
170170
# equivalent: range(n)[::-1], which cython doesn't like in nogil
171-
kh_put_{{ttype}}(table, values[i], &ret)
171+
{{if complex_group}}
172+
value = to_{{c_type}}(values[i])
173+
{{else}}
174+
value = values[i]
175+
{{endif}}
176+
kh_put_{{ttype}}(table, value, &ret)
172177
out[i] = ret == 0
173178
{{endif}}
174179
elif keep == 'first':
@@ -179,7 +184,12 @@ def duplicated_{{dtype}}(const {{c_type}}[:] values, object keep='first'):
179184
{{else}}
180185
with nogil:
181186
for i in range(n):
182-
kh_put_{{ttype}}(table, values[i], &ret)
187+
{{if complex_group}}
188+
value = to_{{c_type}}(values[i])
189+
{{else}}
190+
value = values[i]
191+
{{endif}}
192+
kh_put_{{ttype}}(table, value, &ret)
183193
out[i] = ret == 0
184194
{{endif}}
185195
else:
@@ -197,7 +207,11 @@ def duplicated_{{dtype}}(const {{c_type}}[:] values, object keep='first'):
197207
{{else}}
198208
with nogil:
199209
for i in range(n):
210+
{{if complex_group}}
211+
value = to_{{c_type}}(values[i])
212+
{{else}}
200213
value = values[i]
214+
{{endif}}
201215
k = kh_get_{{ttype}}(table, value)
202216
if k != table.n_buckets:
203217
out[table.vals[k]] = 1
@@ -221,7 +235,7 @@ def duplicated_{{dtype}}(const {{c_type}}[:] values, object keep='first'):
221235
{{if dtype == 'object'}}
222236
def ismember_{{dtype}}(ndarray[{{c_type}}] arr, ndarray[{{c_type}}] values):
223237
{{else}}
224-
def ismember_{{dtype}}(const {{c_type}}[:] arr, const {{c_type}}[:] values):
238+
def ismember_{{dtype}}(const {{dtype}}_t[:] arr, const {{dtype}}_t[:] values):
225239
{{endif}}
226240
"""
227241
Return boolean of values in arr on an
@@ -254,7 +268,12 @@ def ismember_{{dtype}}(const {{c_type}}[:] arr, const {{c_type}}[:] values):
254268
{{else}}
255269
with nogil:
256270
for i in range(n):
257-
kh_put_{{ttype}}(table, values[i], &ret)
271+
{{if complex_group}}
272+
val = to_{{c_type}}(values[i])
273+
{{else}}
274+
val = values[i]
275+
{{endif}}
276+
kh_put_{{ttype}}(table, val, &ret)
258277
{{endif}}
259278

260279
# test membership
@@ -269,7 +288,11 @@ def ismember_{{dtype}}(const {{c_type}}[:] arr, const {{c_type}}[:] values):
269288
{{else}}
270289
with nogil:
271290
for i in range(n):
291+
{{if complex_group}}
292+
val = to_{{c_type}}(arr[i])
293+
{{else}}
272294
val = arr[i]
295+
{{endif}}
273296
k = kh_get_{{ttype}}(table, val)
274297
result[i] = (k != table.n_buckets)
275298
{{endif}}

0 commit comments

Comments
 (0)