Skip to content

Commit 6678574

Browse files
author
manu
committed
BUG: make Series.sort_values(ascending=[False]) behave as ascending=False (pandas-dev#15604)
1 parent a347ecb commit 6678574

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Other enhancements
228228
- ``pd.TimedeltaIndex`` now has a custom datetick formatter specifically designed for nanosecond level precision (:issue:`8711`)
229229
- ``pd.types.concat.union_categoricals`` gained the ``ignore_ordered`` argument to allow ignoring the ordered attribute of unioned categoricals (:issue:`13410`). See the :ref:`categorical union docs <categorical.union>` for more information.
230230
- ``pandas.io.json.json_normalize()`` with an empty ``list`` will return an empty ``DataFrame`` (:issue:`15534`)
231-
231+
- ``Series.sort_values`` accepts a one element list of bool for consistency with the behavior of ``DataFrame.sort_values`` (:issue:`15604`)
232232
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
233233

234234

pandas/core/series.py

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import numpy.ma as ma
1515

1616
from pandas.types.common import (_coerce_to_dtype, is_categorical_dtype,
17+
is_bool,
1718
is_integer, is_integer_dtype,
1819
is_float_dtype,
1920
is_extension_type, is_datetimetz,
@@ -1722,6 +1723,15 @@ def _try_kind_sort(arr):
17221723

17231724
argsorted = _try_kind_sort(arr[good])
17241725

1726+
if is_list_like(ascending):
1727+
if len(ascending) != 1:
1728+
raise ValueError('Length of ascending (%d) must be 1 '
1729+
'for Series' % (len(ascending)))
1730+
ascending = ascending[0]
1731+
1732+
if not is_bool(ascending):
1733+
raise ValueError('ascending must be boolean')
1734+
17251735
if not ascending:
17261736
argsorted = argsorted[::-1]
17271737

pandas/tests/series/test_sorting.py

+19
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ def test_sort_values(self):
6464
ordered = ts.sort_values(ascending=False, na_position='first')
6565
assert_almost_equal(expected, ordered.valid().values)
6666

67+
# ascending=[False] should behave the same as ascending=False
68+
ordered = ts.sort_values(ascending=[False])
69+
expected = ts.sort_values(ascending=False)
70+
assert_series_equal(expected, ordered)
71+
ordered = ts.sort_values(ascending=[False], na_position='first')
72+
expected = ts.sort_values(ascending=False, na_position='first')
73+
assert_series_equal(expected, ordered)
74+
75+
self.assertRaises(ValueError,
76+
lambda: ts.sort_values(ascending=None))
77+
self.assertRaises(ValueError,
78+
lambda: ts.sort_values(ascending=[]))
79+
self.assertRaises(ValueError,
80+
lambda: ts.sort_values(ascending=[1, 2, 3]))
81+
self.assertRaises(ValueError,
82+
lambda: ts.sort_values(ascending=[False, False]))
83+
self.assertRaises(ValueError,
84+
lambda: ts.sort_values(ascending='foobar'))
85+
6786
# inplace=True
6887
ts = self.ts.copy()
6988
ts.sort_values(ascending=False, inplace=True)

0 commit comments

Comments
 (0)