Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ Bug Fixes
across different versions of matplotlib (:issue:`4789`)
- Suppressed DeprecationWarning associated with internal calls issued by repr() (:issue:`4391`)
- Fixed an issue with a duplicate index and duplicate selector with ``.loc`` (:issue:`4825`)
- Fixed an issue with ``DataFrame.sort_index`` where, when sorting by a
single column and passing a list for ``ascending``, the argument for
``ascending`` was being interpreted as ``True`` (:issue:`4839`,
:issue:`4846`)

pandas 0.12.0
-------------
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2856,7 +2856,7 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,

Examples
--------
>>> result = df.sort_index(by=['A', 'B'], ascending=[1, 0])
>>> result = df.sort_index(by=['A', 'B'], ascending=[True, False])

Returns
-------
Expand All @@ -2875,6 +2875,9 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
raise ValueError('When sorting by column, axis must be 0 (rows)')
if not isinstance(by, (tuple, list)):
by = [by]
if com._is_sequence(ascending) and len(by) != len(ascending):
raise ValueError('Length of ascending (%d) != length of by'
' (%d)' % (len(ascending), len(by)))

if len(by) > 1:
keys = []
Expand All @@ -2900,6 +2903,8 @@ def trans(v):
raise ValueError('Cannot sort by duplicate column %s'
% str(by))
indexer = k.argsort(kind=kind)
if isinstance(ascending, (tuple, list)):
ascending = ascending[0]
if not ascending:
indexer = indexer[::-1]
elif isinstance(labels, MultiIndex):
Expand Down
19 changes: 16 additions & 3 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8796,24 +8796,37 @@ def test_sort_index(self):
expected = frame.ix[frame.index[indexer]]
assert_frame_equal(sorted_df, expected)

sorted_df = frame.sort(columns='A', ascending=False)
assert_frame_equal(sorted_df, expected)

# GH4839
sorted_df = frame.sort(columns=['A'], ascending=[False])
assert_frame_equal(sorted_df, expected)

# check for now
sorted_df = frame.sort(columns='A')
assert_frame_equal(sorted_df, expected[::-1])
expected = frame.sort_index(by='A')
assert_frame_equal(sorted_df, expected)

sorted_df = frame.sort(columns='A', ascending=False)
expected = frame.sort_index(by='A', ascending=False)
assert_frame_equal(sorted_df, expected)

sorted_df = frame.sort(columns=['A', 'B'], ascending=False)
expected = frame.sort_index(by=['A', 'B'], ascending=False)
assert_frame_equal(sorted_df, expected)

sorted_df = frame.sort(columns=['A', 'B'])
assert_frame_equal(sorted_df, expected[::-1])

self.assertRaises(ValueError, frame.sort_index, axis=2, inplace=True)

msg = 'When sorting by column, axis must be 0'
with assertRaisesRegexp(ValueError, msg):
frame.sort_index(by='A', axis=1)

msg = r'Length of ascending \(5\) != length of by \(2\)'
with assertRaisesRegexp(ValueError, msg):
frame.sort_index(by=['A', 'B'], axis=0, ascending=[True] * 5)

def test_sort_index_multicolumn(self):
import random
A = np.arange(5).repeat(20)
Expand Down