-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: DataFrame sort columns by rows: sort_values(axis=1) #13622
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 2 commits
970e25b
0f23615
2773cdf
f43ab2e
ea2d89e
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 |
---|---|---|
|
@@ -3127,9 +3127,8 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False, | |
kind='quicksort', na_position='last'): | ||
|
||
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. can you update the |
||
axis = self._get_axis_number(axis) | ||
other_axis = 0 if axis == 1 else 1 | ||
|
||
if axis != 0: | ||
raise ValueError('When sorting by column, axis must be 0 (rows)') | ||
if not isinstance(by, list): | ||
by = [by] | ||
if com.is_sequence(ascending) and len(by) != len(ascending): | ||
|
@@ -3145,7 +3144,7 @@ def trans(v): | |
|
||
keys = [] | ||
for x in by: | ||
k = self[x].values | ||
k = self.xs(x, axis=other_axis).values | ||
if k.ndim == 2: | ||
raise ValueError('Cannot sort by duplicate column %s' % | ||
str(x)) | ||
|
@@ -3157,7 +3156,7 @@ def trans(v): | |
from pandas.core.groupby import _nargsort | ||
|
||
by = by[0] | ||
k = self[by].values | ||
k = self.xs(by, axis=other_axis).values | ||
if k.ndim == 2: | ||
|
||
# try to be helpful | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ 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 | ||
# by column (axis=0) | ||
sorted_df = frame.sort_values(by='A') | ||
indexer = frame['A'].argsort().values | ||
expected = frame.ix[frame.index[indexer]] | ||
|
@@ -116,9 +116,26 @@ def test_sort_values(self): | |
self.assertRaises(ValueError, lambda: frame.sort_values( | ||
by=['A', 'B'], axis=2, inplace=True)) | ||
|
||
msg = 'When sorting by column, axis must be 0' | ||
with assertRaisesRegexp(ValueError, msg): | ||
frame.sort_values(by='A', axis=1) | ||
# by row (axis=1): GH 10806 | ||
sorted_df = frame.sort_values(by=3, axis=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. throw in an 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. Do you mean that the string 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 is what the doc-string should be something like:
which we programatically generate (this is from |
||
expected = frame | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=3, axis=1, ascending=False) | ||
expected = frame.reindex(columns=['C', 'B', 'A']) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=[1, 2], axis=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. add a test for the other options, |
||
expected = frame.reindex(columns=['B', 'A', 'C']) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=[1, 3], axis=1, | ||
ascending=[True, False]) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=[1, 3], axis=1, ascending=False) | ||
expected = frame.reindex(columns=['C', 'B', 'A']) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
msg = r'Length of ascending \(5\) != length of by \(2\)' | ||
with assertRaisesRegexp(ValueError, msg): | ||
|
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 is not clear what actually you are changing. This is for axis 1 support for by
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.
Don't they mean the same thing? (
by
is always requiredsort_values
, and axis 1 is equivalent to "columns".) See if the new wording in 2773cdf hits what you're getting at, otherwise can you please specify?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.
The rewording in that commit is good! (exactly what I wanted to suggest to combine both :-))
Maybe just "with" after "columns"
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.
Done.