-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: Clean up tests of DataFrame.sort_{index,values} #13496
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
Merged
jorisvandenbossche
merged 11 commits into
pandas-dev:master
from
IamJeffG:tst_sort_frame
Jul 11, 2016
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8e9fb62
TST: Clean up tests of DataFrame.sort_{index,values}
IamJeffG 70e06d1
Factor out Series sorting tests to own file.
IamJeffG 67f0ab0
Delegate deprecated sort() and order() to their own tests.
IamJeffG a12cea5
Remove 'by' docstring from Series.sort_values
IamJeffG 600e2de
Document defaults for optional sorting args
IamJeffG 698399d
Move more sort_values, sort_index tests to be together.
IamJeffG f0b6127
Add test for Series.sort_index(sort_remaining=True)
IamJeffG 44dd706
Improve `sort_values` tests when multiple `by`s
IamJeffG 5988277
PEP8 cleanup
IamJeffG f761972
Annotate tests with GH issue
IamJeffG a333162
Fix indentation - docstring string replacement
IamJeffG 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
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 |
---|---|---|
|
@@ -21,75 +21,68 @@ class TestDataFrameSorting(tm.TestCase, TestData): | |
|
||
_multiprocess_can_split_ = True | ||
|
||
def test_sort_values(self): | ||
# API for 9816 | ||
def test_sort_index(self): | ||
# GH13496 | ||
|
||
# sort_index | ||
frame = DataFrame(np.arange(16).reshape(4, 4), index=[1, 2, 3, 4], | ||
columns=['A', 'B', 'C', 'D']) | ||
|
||
# 9816 deprecated | ||
with tm.assert_produces_warning(FutureWarning): | ||
frame.sort(columns='A') | ||
with tm.assert_produces_warning(FutureWarning): | ||
frame.sort() | ||
|
||
# axis=0 : sort rows by index labels | ||
unordered = frame.ix[[3, 2, 4, 1]] | ||
expected = unordered.sort_index() | ||
|
||
result = unordered.sort_index(axis=0) | ||
expected = frame | ||
assert_frame_equal(result, expected) | ||
|
||
unordered = frame.ix[:, [2, 1, 3, 0]] | ||
expected = unordered.sort_index(axis=1) | ||
result = unordered.sort_index(ascending=False) | ||
expected = frame[::-1] | ||
assert_frame_equal(result, expected) | ||
|
||
# axis=1 : sort columns by column names | ||
unordered = frame.ix[:, [2, 1, 3, 0]] | ||
result = unordered.sort_index(axis=1) | ||
assert_frame_equal(result, expected) | ||
assert_frame_equal(result, frame) | ||
|
||
result = unordered.sort_index(axis=1, ascending=False) | ||
expected = frame.ix[:, ::-1] | ||
assert_frame_equal(result, expected) | ||
|
||
# sortlevel | ||
mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC')) | ||
def test_sort_index_multiindex(self): | ||
# GH13496 | ||
|
||
# sort rows by specified level of multi-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. I would make multi-index part of a 2nd test (e.g. separate long tests into multiple ones) |
||
mi = MultiIndex.from_tuples([[2, 1, 3], [1, 1, 1]], names=list('ABC')) | ||
df = DataFrame([[1, 2], [3, 4]], mi) | ||
|
||
result = df.sort_index(level='A', sort_remaining=False) | ||
expected = df.sortlevel('A', sort_remaining=False) | ||
assert_frame_equal(result, expected) | ||
|
||
# sort columns by specified level of multi-index | ||
df = df.T | ||
result = df.sort_index(level='A', axis=1, sort_remaining=False) | ||
expected = df.sortlevel('A', axis=1, sort_remaining=False) | ||
assert_frame_equal(result, expected) | ||
|
||
# MI sort, but no by | ||
# MI sort, but no level: sort_level has no effect | ||
mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC')) | ||
df = DataFrame([[1, 2], [3, 4]], mi) | ||
result = df.sort_index(sort_remaining=False) | ||
expected = df.sort_index() | ||
assert_frame_equal(result, expected) | ||
|
||
def test_sort_index(self): | ||
def test_sort(self): | ||
frame = DataFrame(np.arange(16).reshape(4, 4), index=[1, 2, 3, 4], | ||
columns=['A', 'B', 'C', 'D']) | ||
|
||
# axis=0 | ||
unordered = frame.ix[[3, 2, 4, 1]] | ||
sorted_df = unordered.sort_index(axis=0) | ||
expected = frame | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = unordered.sort_index(ascending=False) | ||
expected = frame[::-1] | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
# axis=1 | ||
unordered = frame.ix[:, ['D', 'B', 'C', 'A']] | ||
sorted_df = unordered.sort_index(axis=1) | ||
expected = frame | ||
assert_frame_equal(sorted_df, expected) | ||
# 9816 deprecated | ||
with tm.assert_produces_warning(FutureWarning): | ||
frame.sort(columns='A') | ||
with tm.assert_produces_warning(FutureWarning): | ||
frame.sort() | ||
|
||
sorted_df = unordered.sort_index(axis=1, ascending=False) | ||
expected = frame.ix[:, ::-1] | ||
assert_frame_equal(sorted_df, expected) | ||
def test_sort_values(self): | ||
frame = DataFrame([[1, 1, 2], [3, 1, 0], [4, 5, 6]], | ||
index=[1, 2, 3], columns=list('ABC')) | ||
|
||
# by column | ||
sorted_df = frame.sort_values(by='A') | ||
|
@@ -109,16 +102,17 @@ def test_sort_index(self): | |
sorted_df = frame.sort_values(by=['A'], ascending=[False]) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
# check for now | ||
sorted_df = frame.sort_values(by='A') | ||
assert_frame_equal(sorted_df, expected[::-1]) | ||
expected = frame.sort_values(by='A') | ||
# multiple bys | ||
sorted_df = frame.sort_values(by=['B', 'C']) | ||
expected = frame.loc[[2, 1, 3]] | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
expected = frame.sort_values(by=['A', 'B'], ascending=False) | ||
sorted_df = frame.sort_values(by=['A', 'B']) | ||
sorted_df = frame.sort_values(by=['B', 'C'], ascending=False) | ||
assert_frame_equal(sorted_df, expected[::-1]) | ||
|
||
sorted_df = frame.sort_values(by=['B', 'A'], ascending=[True, False]) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
self.assertRaises(ValueError, lambda: frame.sort_values( | ||
by=['A', 'B'], axis=2, inplace=True)) | ||
|
||
|
@@ -130,6 +124,25 @@ def test_sort_index(self): | |
with assertRaisesRegexp(ValueError, msg): | ||
frame.sort_values(by=['A', 'B'], axis=0, ascending=[True] * 5) | ||
|
||
def test_sort_values_inplace(self): | ||
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4], | ||
columns=['A', 'B', 'C', 'D']) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by='A', inplace=True) | ||
expected = frame.sort_values(by='A') | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by='A', ascending=False, inplace=True) | ||
expected = frame.sort_values(by='A', ascending=False) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by=['A', 'B'], ascending=False, inplace=True) | ||
expected = frame.sort_values(by=['A', 'B'], ascending=False) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
def test_sort_index_categorical_index(self): | ||
|
||
df = (DataFrame({'A': np.arange(6, dtype='int64'), | ||
|
@@ -361,25 +374,6 @@ def test_sort_index_different_sortorder(self): | |
result = idf['C'].sort_index(ascending=[1, 0]) | ||
assert_series_equal(result, expected['C']) | ||
|
||
def test_sort_inplace(self): | ||
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4], | ||
columns=['A', 'B', 'C', 'D']) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by='A', inplace=True) | ||
expected = frame.sort_values(by='A') | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by='A', ascending=False, inplace=True) | ||
expected = frame.sort_values(by='A', ascending=False) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by=['A', 'B'], ascending=False, inplace=True) | ||
expected = frame.sort_values(by=['A', 'B'], ascending=False) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
def test_sort_index_duplicates(self): | ||
|
||
# with 9816, these are all translated to .sort_values | ||
|
Oops, something went wrong.
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.
add this issue number (the PR number) as a comment (so future readers can reference)