-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: sorting with large float and multiple columns incorrect #14944
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 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1afdbb8
Fix GH 14922
uweschmitt 03699c6
Fix GH 14922
uweschmitt 358a31e
Merge branch 'fix-gh-14922' of github.com:uweschmitt/pandas into fix-…
uweschmitt 21e610c
extended tests + minor cleanup
uweschmitt 04dcbe8
further test cleanup
uweschmitt 60cca5d
add fix of GH14922 to release notes for 0.20.0
uweschmitt 4f28026
fixed typo in whatsnew/v0.20.0.txt
uweschmitt c244438
further cleanup tests
uweschmitt 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
|
||
from pandas.compat import lrange | ||
from pandas import (DataFrame, Series, MultiIndex, Timestamp, | ||
date_range) | ||
date_range, NaT) | ||
|
||
from pandas.util.testing import (assert_series_equal, | ||
assert_frame_equal, | ||
|
@@ -491,3 +491,52 @@ def test_frame_column_inplace_sort_exception(self): | |
|
||
cp = s.copy() | ||
cp.sort_values() # it works! | ||
|
||
def test_sort_nat_values_in_int_column(self): | ||
|
||
# GH 14922: "sorting with large float and multiple columns incorrect" | ||
|
||
# cause was that the int64 value NaT was considered as "na". Which is | ||
# only correct for datetime64 columns. | ||
|
||
int_values = (2, int(NaT)) | ||
float_values = (2.0, -1.797693e308) | ||
|
||
df = DataFrame(dict(int=int_values, float=float_values), | ||
columns=["int", "float"]) | ||
|
||
df_reversed = DataFrame(dict(int=int_values[::-1], | ||
float=float_values[::-1]), | ||
columns=["int", "float"], | ||
index=[1, 0]) | ||
|
||
# NaT is not a "na" for int64 columns, so na_position must not | ||
# influence the result: | ||
df_sorted = df.sort_values(["int", "float"], na_position="last") | ||
assert_frame_equal(df_sorted, df_reversed) | ||
|
||
df_sorted = df.sort_values(["int", "float"], na_position="first") | ||
assert_frame_equal(df_sorted, df_reversed) | ||
|
||
# reverse sorting order | ||
df_sorted = df.sort_values(["int", "float"], ascending=False) | ||
assert_frame_equal(df_sorted, df) | ||
|
||
# and now check if NaT is still considered as "na" for datetime64 | ||
# columns: | ||
df = DataFrame(dict(datetime=[Timestamp("2016-01-01"), NaT], | ||
float=float_values), columns=["datetime", "float"]) | ||
|
||
assert df["datetime"].dtypes == np.dtype("datetime64[ns]"),\ | ||
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. this line here is not needed (the assert of datetime) 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. done. |
||
"this test function is not reliable anymore" | ||
|
||
df_reversed = DataFrame(dict(datetime=[NaT, Timestamp("2016-01-01")], | ||
float=float_values[::-1]), | ||
columns=["datetime", "float"], | ||
index=[1, 0]) | ||
|
||
df_sorted = df.sort_values(["datetime", "float"], na_position="first") | ||
assert_frame_equal(df_sorted, df_reversed) | ||
|
||
df_sorted = df.sort_values(["datetime", "float"], na_position="last") | ||
assert_frame_equal(df_sorted, df_reversed) |
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.
use np.iinfo(np.int64).min instead (and NaT) is already an int
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.
Thought
NaT
is implicitly indicating what went wrong...