Skip to content

COMPAT: safe argsort in Index/Series #17010

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jreback opened this issue Jul 18, 2017 · 2 comments
Closed

COMPAT: safe argsort in Index/Series #17010

jreback opened this issue Jul 18, 2017 · 2 comments
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Closing Candidate May be closeable, needs more eyeballs

Comments

@jreback
Copy link
Contributor

jreback commented Jul 18, 2017

In [1]: Index([0, '1']).sort_values()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-c2949e5a9d7f> in <module>()
----> 1 Index([0, '1']).sort_values()

/Users/jreback/pandas/pandas/core/indexes/base.py in sort_values(self, return_indexer, ascending)
   2026         Return sorted copy of Index
   2027         """
-> 2028         _as = self.argsort()
   2029         if not ascending:
   2030             _as = _as[::-1]

/Users/jreback/pandas/pandas/core/indexes/base.py in argsort(self, *args, **kwargs)
   2089         if result is None:
   2090             result = np.array(self)
-> 2091         return result.argsort(*args, **kwargs)
   2092 
   2093     def __add__(self, other):

TypeError: '>' not supported between instances of 'str' and 'int'
In [7]: Series([0, '1']).sort_values()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/jreback/pandas/pandas/core/series.py in _try_kind_sort(arr)
   1762                 # if kind==mergesort, it can fail for object dtype
-> 1763                 return arr.argsort(kind=kind)
   1764             except TypeError:

TypeError: '>' not supported between instances of 'str' and 'int'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-b0c6ec3dcb93> in <module>()
----> 1 Series([0, '1']).sort_values()

/Users/jreback/pandas/pandas/core/series.py in sort_values(self, axis, ascending, inplace, kind, na_position)
   1775         idx = _default_index(len(self))
   1776 
-> 1777         argsorted = _try_kind_sort(arr[good])
   1778 
   1779         if is_list_like(ascending):

/Users/jreback/pandas/pandas/core/series.py in _try_kind_sort(arr)
   1765                 # stable sort not available for object dtype
   1766                 # uses the argsort default quicksort
-> 1767                 return arr.argsort(kind='quicksort')
   1768 
   1769         arr = self._values

TypeError: '>' not supported between instances of 'str' and 'int'

These can both be fixed by falling back to using .get_indexer() if these fail (.argsort is faster and handles duplicates for object dtypes, so the default is good).

@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Difficulty Intermediate labels Jul 18, 2017
@jreback jreback added this to the Next Major Release milestone Jul 18, 2017
@mroeschke mroeschke added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug labels Apr 4, 2020
@mroeschke mroeschke removed the Compat pandas objects compatability with Numpy or Python functions label Apr 10, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@jbrockmendel
Copy link
Member

I'm not clear on what this is asking for. Do you want sort_values to not raise?

@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label May 3, 2023
@mroeschke
Copy link
Member

Yeah I think these are expected behaviors as of now as we want to be more strict on type comparisons so closing

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Closing Candidate May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

3 participants