-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Compile Factorizer class for all numeric dtypes #49624
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 8 commits
2b0e85e
6969a80
d329b0b
61a5928
85d6c2e
197f7d8
35b99f3
efd6a9e
c2612e1
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 |
---|---|---|
|
@@ -101,6 +101,20 @@ from pandas._libs.khash cimport ( | |
from pandas._libs.tslibs.util cimport get_c_string | ||
from pandas._libs.missing cimport C_NA | ||
|
||
|
||
cdef class Factorizer: | ||
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. is there any particular reason for this to be in the pxi.in instead of the pyx? 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. Had some failures initially, but looks like this was caused by something else. Moved it back |
||
cdef readonly: | ||
Py_ssize_t count | ||
|
||
def __cinit__(self, size_hint: int): | ||
self.count = 0 | ||
|
||
def get_count(self) -> int: | ||
return self.count | ||
|
||
def factorize(self, values, na_sentinel=-1, na_value=None, mask=None) -> np.ndarray: | ||
raise NotImplementedError | ||
|
||
{{py: | ||
|
||
# name, dtype, c_type | ||
|
@@ -876,6 +890,44 @@ cdef class {{name}}HashTable(HashTable): | |
return np.asarray(labels), arr_uniques | ||
{{endif}} | ||
|
||
|
||
cdef class {{name}}Factorizer(Factorizer): | ||
cdef public: | ||
{{name}}HashTable table | ||
{{name}}Vector uniques | ||
|
||
def __cinit__(self, size_hint: int): | ||
self.table = {{name}}HashTable(size_hint) | ||
self.uniques = {{name}}Vector() | ||
|
||
def factorize(self, const {{c_type}}[:] values, | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
na_sentinel=-1, na_value=None, object mask=None) -> np.ndarray: | ||
""" | ||
Returns | ||
------- | ||
ndarray[intp_t] | ||
|
||
Examples | ||
-------- | ||
Factorize values with nans replaced by na_sentinel | ||
|
||
>>> fac = {{name}}Factorizer(3) | ||
>>> fac.factorize(np.array([1,2,3], dtype="{{dtype}}"), na_sentinel=20) | ||
array([0, 1, 2]) | ||
""" | ||
cdef: | ||
ndarray[intp_t] labels | ||
|
||
if self.uniques.external_view_exists: | ||
uniques = {{name}}Vector() | ||
uniques.extend(self.uniques.to_array()) | ||
self.uniques = uniques | ||
labels = self.table.get_labels(values, self.uniques, | ||
self.count, na_sentinel, | ||
na_value=na_value, mask=mask) | ||
self.count = len(self.uniques) | ||
return labels | ||
|
||
{{endfor}} | ||
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.