Skip to content

Commit c9d4e0b

Browse files
manujreback
manu
authored andcommitted
BUG: make Series.sort_values(ascending=[False]) behave as ascending=False (pandas-dev#15604)
closes pandas-dev#15604 Author: manu <[email protected]> Closes pandas-dev#15607 from MLopez-Ibanez/series-ascending and squashes the following commits: 6678574 [manu] BUG: make Series.sort_values(ascending=[False]) behave as ascending=False (pandas-dev#15604)
1 parent 8daf677 commit c9d4e0b

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,
@@ -1719,6 +1720,15 @@ def _try_kind_sort(arr):
17191720

17201721
argsorted = _try_kind_sort(arr[good])
17211722

1723+
if is_list_like(ascending):
1724+
if len(ascending) != 1:
1725+
raise ValueError('Length of ascending (%d) must be 1 '
1726+
'for Series' % (len(ascending)))
1727+
ascending = ascending[0]
1728+
1729+
if not is_bool(ascending):
1730+
raise ValueError('ascending must be boolean')
1731+
17221732
if not ascending:
17231733
argsorted = argsorted[::-1]
17241734

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)