Skip to content

Commit 85f41db

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

File tree

3 files changed

+15
-1
lines changed

3 files changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
is_datetime64tz_dtype,
2222
is_timedelta64_dtype,
2323
is_list_like,
24+
is_sequence,
2425
is_hashable,
2526
is_iterator,
2627
is_dict_like,
@@ -1722,6 +1723,12 @@ def _try_kind_sort(arr):
17221723

17231724
argsorted = _try_kind_sort(arr[good])
17241725

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

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)