Skip to content

Commit 3ca92d8

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

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-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 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

+6
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,12 @@ def _try_kind_sort(arr):
17221722

17231723
argsorted = _try_kind_sort(arr[good])
17241724

1725+
if is_sequence(ascending):
1726+
if len(ascending) > 1:
1727+
raise ValueError('Length of ascending (%d) cannot be larger '
1728+
'than 1 for Series' % (len(ascending)))
1729+
ascending = ascending[0]
1730+
17251731
if not ascending:
17261732
argsorted = argsorted[::-1]
17271733

pandas/tests/series/test_sorting.py

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ 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+
6774
# inplace=True
6875
ts = self.ts.copy()
6976
ts.sort_values(ascending=False, inplace=True)

0 commit comments

Comments
 (0)