Skip to content

ENH: Add Series.sort ascending keyword #3630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ def dot(self, other):

Parameters
----------
other : Series or DataFrame
other : Series or DataFrame

Returns
-------
Expand Down Expand Up @@ -2194,7 +2194,7 @@ def update(self, other):
#----------------------------------------------------------------------
# Reindexing, sorting

def sort(self, axis=0, kind='quicksort', order=None):
def sort(self, axis=0, kind='quicksort', order=None, ascending=True):
"""
Sort values and index labels by value, in place. For compatibility with
ndarray API. No return value
Expand All @@ -2206,8 +2206,15 @@ def sort(self, axis=0, kind='quicksort', order=None):
Choice of sorting algorithm. See np.sort for more
information. 'mergesort' is the only stable algorithm
order : ignored
ascending : boolean, default True
Sort ascending. Passing False sorts descending

See Also
--------
pandas.Series.order
"""
sortedSeries = self.order(na_last=True, kind=kind)
sortedSeries = self.order(na_last=True, kind=kind,
ascending=ascending)

true_base = self
while true_base.base is not None:
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def test_constructor_dtype_datetime64(self):
np.datetime64(datetime(2013, 1, 2)),
np.datetime64(datetime(2013, 1, 3)),
]

s = Series(dates)
self.assert_(s.dtype == 'M8[ns]')

Expand Down Expand Up @@ -1162,7 +1162,7 @@ def test_where(self):
s = Series(np.arange(10), dtype=dtype)
mask = s < 5
values = [2.5,3.5,4.5,5.5,6.5]
s[mask] = values
s[mask] = values
expected = Series(values + range(5,10), dtype='float64')
assert_series_equal(s, expected)
self.assertEquals(s.dtype, expected.dtype)
Expand Down Expand Up @@ -2591,7 +2591,7 @@ def test_dot(self):
expected = Series(np.dot(a.values, b.values),
index=['1', '2', '3'])
assert_series_equal(result, expected)

#Check index alignment
b2 = b.reindex(index=reversed(b.index))
result = a.dot(b)
Expand Down Expand Up @@ -2723,6 +2723,11 @@ def test_sort(self):
self.assert_(np.array_equal(ts, self.ts.order()))
self.assert_(np.array_equal(ts.index, self.ts.order().index))

ts.sort(ascending=False)
self.assert_(np.array_equal(ts, self.ts.order(ascending=False)))
self.assert_(np.array_equal(ts.index,
self.ts.order(ascending=False).index))

def test_sort_index(self):
import random

Expand Down