-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: support EAs in nargsort without warning #25595
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
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 |
---|---|---|
|
@@ -8,7 +8,9 @@ | |
|
||
from pandas.core.dtypes.cast import infer_dtype_from_array | ||
from pandas.core.dtypes.common import ( | ||
ensure_int64, ensure_platform_int, is_categorical_dtype, is_list_like) | ||
ensure_int64, ensure_platform_int, is_categorical_dtype, is_list_like, | ||
is_extension_array_dtype) | ||
from pandas.core.dtypes.generic import ABCIndexClass | ||
from pandas.core.dtypes.missing import isna | ||
|
||
import pandas.core.algorithms as algorithms | ||
|
@@ -239,7 +241,9 @@ def nargsort(items, kind='quicksort', ascending=True, na_position='last'): | |
""" | ||
|
||
# specially handle Categorical | ||
if is_categorical_dtype(items): | ||
if is_extension_array_dtype(items): | ||
if isinstance(items, ABCIndexClass): | ||
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. Hmm, so when we have ExtensionArrays-backed indexes in the future, this is liable to break? Or we'll need a I don't suppose 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.
No, because we want to dispatch to the 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. Sorry, I mis-remembered where the warning was coming from (I thought it was implemented on DatetimeArray). 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. why exactly is this change needed? should this not just defer to the index 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. The |
||
items = items._values | ||
if na_position not in {'first', 'last'}: | ||
raise ValueError('invalid na_position: {!r}'.format(na_position)) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,9 @@ | |
|
||
from pandas.compat import PY2 | ||
|
||
from pandas import DataFrame, MultiIndex, Series, compat, concat, merge | ||
from pandas import ( | ||
Categorical, DataFrame, MultiIndex, Series, compat, | ||
concat, merge, to_datetime) | ||
from pandas.core import common as com | ||
from pandas.core.sorting import ( | ||
decons_group_index, get_group_index, is_int64_overflow_possible, | ||
|
@@ -183,6 +185,22 @@ def test_nargsort(self): | |
exp = list(range(5)) + list(range(105, 110)) + list(range(104, 4, -1)) | ||
tm.assert_numpy_array_equal(result, np.array(exp), check_dtype=False) | ||
|
||
@pytest.mark.parametrize('data', [ | ||
Categorical(['a', 'c', 'a', 'b']), | ||
to_datetime([0, 2, 0, 1]).tz_localize('Europe/Brussels')]) | ||
def test_nargsort_extension_array(self, data): | ||
result = nargsort(data) | ||
expected = np.array([0, 2, 3, 1], dtype=np.intp) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_nargsort_datetimearray_warning(self, recwarn): | ||
# https://github.com/pandas-dev/pandas/issues/25439 | ||
# can be removed once the FutureWarning for np.array(DTA) is removed | ||
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 a reason you are not using 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. Because I want to assert that a specific warning is not raised. I found it a bit clunky as well, so if you know a better way, all ears. 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.
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. Assuming you mean
giving basically the same as I did above (but above I did it with the recwarn fixture instead of the with statement. Happy to use the with statement if that is more typically used / easier to understand. Actually now thinking about it, I would find that clearer myself :-)) 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, I meant In theory, you could rely on our global "fail on warning" setting from the one CI job, but I don't think I trust that enough :) 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.
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. But not for a specific warning, what I tried to do here. Will change to 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.
sure, but try to guard against unexpected warnings is a bit of a rabbit hole |
||
data = to_datetime([0, 2, 0, 1]).tz_localize('Europe/Brussels') | ||
nargsort(data) | ||
msg = "Converting timezone-aware DatetimeArray to timezone-naive" | ||
assert len([w for w in recwarn.list if msg in str(w.message)]) == 0 | ||
|
||
|
||
class TestMerge(object): | ||
|
||
|
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.
This comment is slightly stale now.