8000 Merge pull request #4846 from jtratner/GH4839_sort_index_ascending_bug · pandas-dev/pandas@2f0cc87 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f0cc87

Browse files
committed
Merge pull request #4846 from jtratner/GH4839_sort_index_ascending_bug
BUG: fix sort_index with one col and ascending list
2 parents ec28c9d + 1fb1bab commit 2f0cc87

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

doc/source/release.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ Bug Fixes
424424
across different versions of matplotlib (:issue:`4789`)
425425
- Suppressed DeprecationWarning associated with internal calls issued by repr() (:issue:`4391`)
426426
- Fixed an issue with a duplicate index and duplicate selector with ``.loc`` (:issue:`4825`)
427+
- Fixed an issue with ``DataFrame.sort_index`` where, when sorting by a
428+
single column and passing a list for ``ascending``, the argument for
429+
``ascending`` was being interpreted as ``True`` (:issue:`4839`,
430+
:issue:`4846`)
427431

428432
pandas 0.12.0
429433
-------------

pandas/core/frame.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,7 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
28562856
28572857
Examples
28582858
--------
2859-
>>> result = df.sort_index(by=['A', 'B'], ascending=[1, 0])
2859+
>>> result = df.sort_index(by=['A', 'B'], ascending=[True, False])
28602860
28612861
Returns
28622862
-------
@@ -2875,6 +2875,9 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
28752875
raise ValueError('When sorting by column, axis must be 0 (rows)')
28762876
if not isinstance(by, (tuple, list)):
28772877
by = [by]
2878+
if com._is_sequence(ascending) and len(by) != len(ascending):
2879+
raise ValueError('Length of ascending (%d) != length of by'
2880+
' (%d)' % (len(ascending), len(by)))
28782881

28792882
if len(by) > 1:
28802883
keys = []
@@ -2900,6 +2903,8 @@ def trans(v):
29002903
raise ValueError('Cannot sort by duplicate column %s'
29012904
% str(by))
29022905
indexer = k.argsort(kind=kind)
2906+
if isinstance(ascending, (tuple, list)):
2907+
ascending = ascending[0]
29032908
if not ascending:
29042909
indexer = indexer[::-1]
29052910
elif isinstance(labels, MultiIndex):

pandas/tests/test_frame.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8796,24 +8796,37 @@ def test_sort_index(self):
87968796
expected = frame.ix[frame.index[indexer]]
87978797
assert_frame_equal(sorted_df, expected)
87988798

8799+
sorted_df = frame.sort(columns='A', ascending=False)
8800+
assert_frame_equal(sorted_df, expected)
8801+
8802+
# GH4839
8803+
sorted_df = frame.sort(columns=['A'], ascending=[False])
8804+
assert_frame_equal(sorted_df, expected)
8805+
87998806
# check for now
88008807
sorted_df = frame.sort(columns='A')
8808+
assert_frame_equal(sorted_df, expected[::-1])
88018809
expected = frame.sort_index(by='A')
88028810
assert_frame_equal(sorted_df, expected)
88038811

8804-
sorted_df = frame.sort(columns='A', ascending=False)
8805-
expected = frame.sort_index(by='A', ascending=False)
8806-
assert_frame_equal(sorted_df, expected)
88078812

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

8817+
sorted_df = frame.sort(columns=['A', 'B'])
8818+
assert_frame_equal(sorted_df, expected[::-1])
8819+
88128820
self.assertRaises(ValueError, frame.sort_index, axis=2, inplace=True)
8821+
88138822
msg = 'When sorting by column, axis must be 0'
88148823
with assertRaisesRegexp(ValueError, msg):
88158824
frame.sort_index(by='A', axis=1)
88168825

8826+
msg = r'Length of ascending \(5\) != length of by \(2\)'
8827+
with assertRaisesRegexp(ValueError, msg):
8828+
frame.sort_index(by=['A', 'B'], axis=0, ascending=[True] * 5)
8829+
88178830
def test_sort_index_multicolumn(self):
88188831
import random
88198832
A = np.arange(5).repeat(20)

0 commit comments

Comments
 (0)
0