-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
COMPAT: Argsort position matches NumPy #31210
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,10 +191,8 @@ def sort_values(self, return_indexer=False, ascending=True): | |
sorted_index = self.take(_as) | ||
return sorted_index, _as | ||
else: | ||
# NB: using asi8 instead of _ndarray_values matters in numpy 1.18 | ||
# because the treatment of NaT has been changed to put NaT last | ||
# instead of first. | ||
sorted_values = np.sort(self.asi8) | ||
values = self._data | ||
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. maybe use self._data.values_for_argsort? |
||
sorted_values = np.sort(values.view("M8[ns]")).view("i8") | ||
|
||
freq = self.freq | ||
if freq is not None and not is_period_dtype(self): | ||
|
@@ -224,6 +222,9 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): | |
self, indices, axis, allow_fill, fill_value, **kwargs | ||
) | ||
|
||
def argsort(self, *args, **kwargs) -> np.ndarray: | ||
return np.argsort(self._data, *args, **kwargs) | ||
|
||
@Appender(_shared_docs["searchsorted"]) | ||
def searchsorted(self, value, side="left", sorter=None): | ||
if isinstance(value, str): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -795,3 +795,11 @@ def test_to_numpy_extra(array): | |
assert result[0] == result[1] | ||
|
||
tm.assert_equal(array, original) | ||
|
||
|
||
@pytest.mark.parametrize("dtype", ["datetime64[ns]", "timedelta64[ns]", "Period[D]"]) | ||
def test_argsort(dtype): | ||
a = pd.array([2001, pd.NaT, 2000], dtype=dtype) | ||
result = a.argsort() | ||
expected = np.array([2, 0, 1]) | ||
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. im a bit confused by this. how is this prevented from varying based on the numpy version? |
||
tm.assert_numpy_array_equal(result, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@jbrockmendel moved this down to the arrays. Basic idea is to just sort as datetime64[ns], which should be correct for datetime, timedelta, and period.
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.
sounds good, thanks