Skip to content

Commit a711db0

Browse files
committed
BUG: Series.argsort failing on datetime64[ns] when NaT present, GH #2967
1 parent 50a3c96 commit a711db0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pandas 0.11.0
125125
- Support idxmin/idxmax in a Series (but with no NaT)
126126

127127
- Bug on in-place putmasking on an ``integer`` series that needs to be converted to ``float`` (GH2746_)
128+
- Bug in argsort of ``datetime64[ns]`` Series with ``NaT`` (GH2967_)
128129

129130
.. _GH622: https://github.com/pydata/pandas/issues/622
130131
.. _GH797: https://github.com/pydata/pandas/issues/797
@@ -145,6 +146,7 @@ pandas 0.11.0
145146
.. _GH2909: https://github.com/pydata/pandas/issues/2909
146147
.. _GH2931: https://github.com/pydata/pandas/issues/2931
147148
.. _GH2973: https://github.com/pydata/pandas/issues/2973
149+
.. _GH2967: https://github.com/pydata/pandas/issues/2967
148150

149151

150152
pandas 0.10.1

pandas/core/series.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2092,16 +2092,17 @@ def argsort(self, axis=0, kind='quicksort', order=None):
20922092
20932093
Returns
20942094
-------
2095-
argsorted : Series
2095+
argsorted : Series, with -1 indicated where nan values are present
2096+
20962097
"""
20972098
values = self.values
20982099
mask = isnull(values)
20992100

21002101
if mask.any():
2101-
result = values.copy()
2102+
result = Series(-1,index=self.index,name=self.name)
21022103
notmask = -mask
2103-
result[notmask] = np.argsort(values[notmask], kind=kind)
2104-
return Series(result, index=self.index, name=self.name)
2104+
result.values[notmask] = np.argsort(self.values[notmask], kind=kind)
2105+
return result
21052106
else:
21062107
return Series(np.argsort(values, kind=kind), index=self.index,
21072108
name=self.name)

pandas/tests/test_series.py

+15
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,21 @@ def test_argsort(self):
14041404
argsorted = self.ts.argsort()
14051405
self.assert_(issubclass(argsorted.dtype.type, np.integer))
14061406

1407+
# GH 2967 (introduced bug in 0.11-dev I think)
1408+
s = Series([Timestamp('201301%02d'% (i+1)) for i in range(5)])
1409+
self.assert_(s.dtype == 'datetime64[ns]')
1410+
shifted = s.shift(-1)
1411+
self.assert_(shifted.dtype == 'datetime64[ns]')
1412+
self.assert_(isnull(shifted[4]) == True)
1413+
1414+
result = s.argsort()
1415+
expected = Series(range(5),dtype='int64')
1416+
assert_series_equal(result,expected)
1417+
1418+
result = shifted.argsort()
1419+
expected = Series(range(4) + [-1],dtype='int64')
1420+
assert_series_equal(result,expected)
1421+
14071422
def test_argsort_stable(self):
14081423
s = Series(np.random.randint(0, 100, size=10000))
14091424
mindexer = s.argsort(kind='mergesort')

0 commit comments

Comments
 (0)