Skip to content

Commit 3a28948

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

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 1 deletion
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ 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 = np.sort(ts.valid().values)[::-1]
70+
assert_almost_equal(expected, ordered.valid().values)
71+
ordered = ts.sort_values(ascending=[False], na_position='first')
72+
assert_almost_equal(expected, ordered.valid().values)
73+
74+
self.assertRaises(ValueError,
75+
lambda: ts.sort_values(ascending=None))
76+
self.assertRaises(ValueError,
77+
lambda: ts.sort_values(ascending=[]))
78+
self.assertRaises(ValueError,
79+
lambda: ts.sort_values(ascending=[1, 2, 3]))
80+
self.assertRaises(ValueError,
81+
lambda: ts.sort_values(ascending=[False, False]))
82+
self.assertRaises(ValueError,
83+
lambda: ts.sort_values(ascending='foobar'))
84+
6785
# inplace=True
6886
ts = self.ts.copy()
6987
ts.sort_values(ascending=False, inplace=True)

0 commit comments

Comments
 (0)