Permalink
Browse files

added ascending argument to Series.order

  • Loading branch information...
1 parent 825ebf3 commit a9fcf81e49d2b8287f4f6e160f4d07ec648d35e3 @wesm wesm committed Jun 30, 2011
Showing with 19 additions and 6 deletions.
  1. +9 −3 pandas/core/series.py
  2. +8 −1 pandas/tests/test_series.py
  3. +2 −2 test.sh
View
@@ -863,7 +863,7 @@ def argsort(self, axis=0, kind='quicksort', order=None):
else:
return Series(np.argsort(values), index=self.index)
- def order(self, na_last=True, **kwds):
+ def order(self, na_last=True, ascending=True, **kwds):
"""
Sorts Series object, by value, maintaining index-value object
@@ -894,13 +894,19 @@ def _try_mergesort(arr):
good = -bad
idx = np.arange(len(self))
+
+ argsorted = _try_mergesort(arr[good])
+
+ if not ascending:
+ argsorted = argsorted[::-1]
+
if na_last:
n = sum(good)
- sortedIdx[:n] = idx[good][_try_mergesort(arr[good])]
+ sortedIdx[:n] = idx[good][argsorted]
sortedIdx[n:] = idx[bad]
else:
n = sum(bad)
- sortedIdx[n:] = idx[good][_try_mergesort(arr[good])]
+ sortedIdx[n:] = idx[good][argsorted]
sortedIdx[:n] = idx[bad]
return Series(arr[sortedIdx], index=self.index[sortedIdx])
@@ -10,7 +10,7 @@
from pandas import Index, Series, TimeSeries, DataFrame, isnull
import pandas.core.datetools as datetools
-from pandas.util.testing import assert_series_equal
+from pandas.util.testing import assert_series_equal, assert_almost_equal
import pandas.util.testing as common
#-------------------------------------------------------------------------------
@@ -623,6 +623,13 @@ def test_order(self):
# no failure
ser.order()
+ # ascending=False
+ ordered = ts.order(ascending=False)
+ expected = np.sort(ts.valid().values)[::-1]
+ assert_almost_equal(expected, ordered.valid().values)
+ ordered = ts.order(ascending=False, na_last=False)
+ assert_almost_equal(expected, ordered.valid().values)
+
def test_map(self):
result = self.ts.map(lambda x: x * 2)
View
@@ -1,7 +1,7 @@
#!/bin/sh
coverage erase
-# nosetests -w pandas --with-coverage --cover-package=pandas --pdb-failure --pdb
-nosetests -w pandas/io --with-coverage --cover-package=pandas.io --pdb-failure --pdb
+nosetests -w pandas --with-coverage --cover-package=pandas --pdb-failure --pdb
+# nosetests -w pandas/io --with-coverage --cover-package=pandas.io --pdb-failure --pdb
# nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure --pdb
# nosetests -w pandas/stats --with-coverage --cover-package=pandas.stats
# coverage run runtests.py

0 comments on commit a9fcf81

Please sign in to comment.