Skip to content

categorical needs a searchsorted implmentation #8420

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

Closed
jankatins opened this issue Sep 29, 2014 · 4 comments
Closed

categorical needs a searchsorted implmentation #8420

jankatins opened this issue Sep 29, 2014 · 4 comments
Labels
Categorical Categorical Data Type Enhancement
Milestone

Comments

@jankatins
Copy link
Contributor

Currently the Categorical.searchsorted() raises NotImplementedError.

@jreback jreback added Categorical Categorical Data Type Enhancement labels Sep 29, 2014
@jreback jreback added this to the 0.16 milestone Sep 29, 2014
@jreback
Copy link
Contributor

jreback commented Sep 29, 2014

someting like:

def searchsorted(self, value, side='left'):
     if not self.ordered:
         raise ValueError("searchsorted requires an ordered Categorical")
     index = self.values.searchsorted(value, side=side)
     return index

?

@jankatins
Copy link
Contributor Author

Nope, along this lines

if not self.ordered:
    raise ValueError("searchsorted requires an ordered Categorical")
values_as_codes =_get_codes_for_values(values, self.categories) 
# or ... = self.categories.get_indexer(value)
index = np.searchsorted(self.codes, values_as_codes, side=side)
return index

@stevesimmons
Copy link
Contributor

I have a fix for this in pull request #8928.

    >>> x = pd.Categorical(['apple', 'bread', 'bread', 'cheese', 'milk', 'donuts' ])
    >>> x.searchsorted(['bread', 'eggs'], side='right', sorter=[0, 1, 2, 3, 5, 4])
    # array([3, 5]) # eggs after donuts, after switching milk and donuts

    values_as_codes = self.categories.values.searchsorted(Series(v).values, side)
    indices = self.codes.searchsorted(values_as_codes, sorter=sorter)
    return indices

@jreback
Copy link
Contributor

jreback commented Dec 5, 2014

closed by #8972

@jreback jreback closed this as completed Dec 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Enhancement
Projects
None yet
3 participants