Skip to content

Commit 4d22954

Browse files
committed
BUG: pass on sort kind from Series.sort to order and in argsort GH #668
1 parent 2e61d97 commit 4d22954

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ pandas 0.7.0
223223
- Fix out-of-bounds segfault in pad_object and backfill_object methods when
224224
either source or target array are empty
225225
- Could not create a new column in a DataFrame from a list of tuples
226+
- Fix bugs preventing SparseDataFrame and SparseSeries working with groupby
227+
(GH #666)
226228

227229
Thanks
228230
------

pandas/core/series.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -1356,10 +1356,18 @@ def combine_first(self, other):
13561356

13571357
def sort(self, axis=0, kind='quicksort', order=None):
13581358
"""
1359-
Sort values and index labels in place, for compatibility with
1360-
ndarray. No return value
1359+
Sort values and index labels by value, in place. For compatibility with
1360+
ndarray API. No return value
1361+
1362+
Parameters
1363+
----------
1364+
axis : int (can only be zero)
1365+
kind : {'mergesort', 'quicksort', 'heapsort'}, default 'quicksort'
1366+
Choice of sorting algorithm. See np.sort for more
1367+
information. 'mergesort' is the only stable algorithm
1368+
order : ignored
13611369
"""
1362-
sortedSeries = self.order(na_last=True)
1370+
sortedSeries = self.order(na_last=True, kind=kind)
13631371

13641372
true_base = self
13651373
while true_base.base is not None:
@@ -1399,6 +1407,14 @@ def argsort(self, axis=0, kind='quicksort', order=None):
13991407
Overrides ndarray.argsort. Argsorts the value, omitting NA/null values,
14001408
and places the result in the same locations as the non-NA values
14011409
1410+
Parameters
1411+
----------
1412+
axis : int (can only be zero)
1413+
kind : {'mergesort', 'quicksort', 'heapsort'}, default 'quicksort'
1414+
Choice of sorting algorithm. See np.sort for more
1415+
information. 'mergesort' is the only stable algorithm
1416+
order : ignored
1417+
14021418
Returns
14031419
-------
14041420
argsorted : Series
@@ -1409,10 +1425,11 @@ def argsort(self, axis=0, kind='quicksort', order=None):
14091425
if mask.any():
14101426
result = values.copy()
14111427
notmask = -mask
1412-
result[notmask] = np.argsort(values[notmask])
1428+
result[notmask] = np.argsort(values[notmask], kind=kind)
14131429
return Series(result, index=self.index, name=self.name)
14141430
else:
1415-
return Series(np.argsort(values), index=self.index, name=self.name)
1431+
return Series(np.argsort(values, kind=kind), index=self.index,
1432+
name=self.name)
14161433

14171434
def rank(self):
14181435
"""
@@ -1429,7 +1446,7 @@ def rank(self):
14291446
ranks = lib.rank_1d_generic(self.values)
14301447
return Series(ranks, index=self.index, name=self.name)
14311448

1432-
def order(self, na_last=True, ascending=True):
1449+
def order(self, na_last=True, ascending=True, kind='mergesort'):
14331450
"""
14341451
Sorts Series object, by value, maintaining index-value link
14351452
@@ -1439,6 +1456,9 @@ def order(self, na_last=True, ascending=True):
14391456
Put NaN's at beginning or end
14401457
ascending : boolean, default True
14411458
Sort ascending. Passing False sorts descending
1459+
kind : {'mergesort', 'quicksort', 'heapsort'}, default 'mergesort'
1460+
Choice of sorting algorithm. See np.sort for more
1461+
information. 'mergesort' is the only stable algorith
14421462
14431463
Returns
14441464
-------

pandas/tests/test_series.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -873,14 +873,26 @@ def test_argsort(self):
873873
argsorted = self.ts.argsort()
874874
self.assert_(issubclass(argsorted.dtype.type, np.integer))
875875

876+
def test_argsort_stable(self):
877+
s = Series(np.random.randint(0, 100, size=10000))
878+
mindexer = s.argsort(kind='mergesort')
879+
qindexer = s.argsort()
880+
881+
mexpected = np.argsort(s.values, kind='mergesort')
882+
qexpected = np.argsort(s.values, kind='quicksort')
883+
884+
self.assert_(np.array_equal(mindexer, mexpected))
885+
self.assert_(np.array_equal(qindexer, qexpected))
886+
self.assert_(not np.array_equal(qindexer, mindexer))
887+
876888
def test_cumsum(self):
877889
self._check_accum_op('cumsum')
878890

879891
def test_cumprod(self):
880892
self._check_accum_op('cumprod')
881893

882894
def test_cummin(self):
883-
self.assert_(np.array_equal(self.ts.cummin(),
895+
self.assert_(np.array_equal(self.ts.cummin(),
884896
np.minimum.accumulate(np.array(self.ts))))
885897
ts = self.ts.copy()
886898
ts[::2] = np.NaN
@@ -890,7 +902,7 @@ def test_cummin(self):
890902
self.assert_(np.array_equal(result, expected))
891903

892904
def test_cummax(self):
893-
self.assert_(np.array_equal(self.ts.cummax(),
905+
self.assert_(np.array_equal(self.ts.cummax(),
894906
np.maximum.accumulate(np.array(self.ts))))
895907
ts = self.ts.copy()
896908
ts[::2] = np.NaN

0 commit comments

Comments
 (0)