-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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 all 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 |
---|---|---|
|
@@ -2061,27 +2061,25 @@ def safe_sort( | |
dtype, _ = infer_dtype_from_array(values) | ||
values = np.asarray(values, dtype=dtype) | ||
|
||
def sort_mixed(values): | ||
# order ints before strings, safe in py3 | ||
str_pos = np.array([isinstance(x, str) for x in values], dtype=bool) | ||
nums = np.sort(values[~str_pos]) | ||
strs = np.sort(values[str_pos]) | ||
return np.concatenate([nums, np.asarray(strs, dtype=object)]) | ||
|
||
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) | ||
ordered = _sort_mixed(values) | ||
else: | ||
try: | ||
sorter = values.argsort() | ||
ordered = values.take(sorter) | ||
except TypeError: | ||
# try this anyway | ||
ordered = sort_mixed(values) | ||
# Previous sorters failed or were not applicable, try `_sort_mixed` | ||
# which would work, but which fails for special case of 1d arrays | ||
# with tuples. | ||
if values.size and isinstance(values[0], tuple): | ||
ordered = _sort_tuples(values) | ||
else: | ||
ordered = _sort_mixed(values) | ||
|
||
# codes: | ||
|
||
|
@@ -2128,3 +2126,26 @@ def sort_mixed(values): | |
np.putmask(new_codes, mask, na_sentinel) | ||
|
||
return ordered, ensure_platform_int(new_codes) | ||
|
||
|
||
def _sort_mixed(values): | ||
""" order ints before strings in 1d arrays, safe in py3 """ | ||
str_pos = np.array([isinstance(x, str) for x in values], dtype=bool) | ||
nums = np.sort(values[~str_pos]) | ||
strs = np.sort(values[str_pos]) | ||
return np.concatenate([nums, np.asarray(strs, dtype=object)]) | ||
|
||
|
||
def _sort_tuples(values: np.ndarray[tuple]): | ||
""" | ||
Convert array of tuples (1d) to array or array (2d). | ||
We need to keep the columns separately as they contain different types and | ||
nans (can't use `np.sort` as it may fail when str and nan are mixed in a | ||
column as types cannot be compared). | ||
""" | ||
from pandas.core.internals.construction import to_arrays | ||
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. @ssche two completely contradictory questions/requests
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.
My first attempt (1320ff1) wasn't using core.internals, as far as I recall, but it was deemed too complex.
Maybe, what are you trying to achieve? 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.
Just simplification/de-duplication 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. I don't know if this is still relevant (I forgot to respond), but I would argue that Anyway, I hope you could proceed with your much appreciated simplification/de-dupe efforts... |
||
from pandas.core.sorting import lexsort_indexer | ||
|
||
arrays, _ = to_arrays(values, None) | ||
indexer = lexsort_indexer(arrays, orders=True) | ||
return values[indexer] |
Uh oh!
There was an error while loading. Please reload this page.