-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Gh 36562 typeerror comparison not supported between float and str #37096
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 15 commits
1320ff1
a1b9385
1f76d21
7076841
225675d
2677166
6f476bc
3688238
aba429c
8ae9279
dd5a38d
7b7d6f8
4226662
8cbfa01
6d71000
92e1e33
3ada9ce
95670f1
d9dcd22
66c30c7
db66528
16ae4f4
9d03dc3
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
""" | ||
from __future__ import annotations | ||
|
||
import functools | ||
import operator | ||
from textwrap import dedent | ||
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union, cast | ||
|
@@ -2070,20 +2071,63 @@ def sort_mixed(values): | |
strs = np.sort(values[str_pos]) | ||
return np.concatenate([nums, np.asarray(strs, dtype=object)]) | ||
|
||
def sort_tuples(values): | ||
# sorts tuples with mixed values. can handle nan vs string comparisons. | ||
def cmp_func(index_x, index_y): | ||
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. could do 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. yes let's change the name and please add typing for cmp_func and sort_tuples. (sort_mixed if you can as well :->) |
||
x = values[index_x] | ||
y = values[index_y] | ||
# shortcut loop in case both tuples are the same | ||
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. you don't need to do any of this see safe_sort 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. what are you saying? what isn't needed? this PR is changing 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. and how does this not do it now! iirc this is adding a lot of non performing code for the purpose of fixing the error message? 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.
now a TypeError will be raised in
in the event of all other (more performant) sorters failing, this (unarguably slower) sorter will be used. this should not compromise any other use case's performance, but at least makes the code in the ticket description succeed (slower, but at least not failing). 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. @jreback I moved the slow |
||
if x == y: | ||
return 0 | ||
# lexicographic sorting | ||
for i in range(max(len(x), len(y))): | ||
# check if the tuples have different lengths (shorter tuples | ||
# first) | ||
if i >= len(x): | ||
return -1 | ||
if i >= len(y): | ||
return +1 | ||
x_is_na = isna(x[i]) | ||
y_is_na = isna(y[i]) | ||
# values are the same -> resolve tie with next element | ||
if (x_is_na and y_is_na) or (x[i] == y[i]): | ||
continue | ||
# check for nan values (sort nan to the end) | ||
if x_is_na and not y_is_na: | ||
return +1 | ||
if not x_is_na and y_is_na: | ||
return -1 | ||
# normal greater/less than comparison | ||
if x[i] < y[i]: | ||
return -1 | ||
return +1 | ||
# both values are the same (should already have been caught) | ||
return 0 | ||
|
||
ixs = np.arange(len(values)) | ||
ixs = sorted(ixs, key=functools.cmp_to_key(cmp_func)) | ||
return values[ixs] | ||
|
||
sorter = None | ||
|
||
if ( | ||
not is_extension_array_dtype(values) | ||
and lib.infer_dtype(values, skipna=False) == "mixed-integer" | ||
): | ||
# unorderable in py3 if mixed str/int | ||
ordered = sort_mixed(values) | ||
else: | ||
try: | ||
sorter = values.argsort() | ||
ordered = values.take(sorter) | ||
except TypeError: | ||
# try this anyway | ||
ordered = sort_mixed(values) | ||
try: | ||
ssche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ordered = sort_mixed(values) | ||
except TypeError: | ||
if values.size and isinstance(values[0], tuple): | ||
ordered = sort_tuples(values) | ||
else: | ||
raise | ||
|
||
# codes: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you simply try to remove the nan's from values and add them to the end is fine and just fix up sort_mixed, we do this in a number of places e.g. something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started off with trying to fix
sort_mixed
and removed nans as you suggested and would have loved to use it instead. however the structure of the array doesn't allow for that (we are talking about a 1-d array of tuples, i.e. dtype object, not an N-d array which would allow for all those vector operations to be applicable).here's the data of
values
that gets passed in tosort_tuples()
when running the test I added:I could convert
value
to a N-d array (and usesort_mixed
), but that solution would come with its own overhead costs...your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any feedback on the above, @jreback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i care about code complexity here, this is adding a lot, try this