Skip to content

BUG: Series.argsort failing on datetime64[ns] when NaT present, GH #2967 #2977

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 1 commit into from
Mar 6, 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
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pandas 0.11.0
- Support idxmin/idxmax in a Series (but with no NaT)

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

.. _GH622: https://github.com/pydata/pandas/issues/622
.. _GH797: https://github.com/pydata/pandas/issues/797
Expand All @@ -145,6 +146,7 @@ pandas 0.11.0
.. _GH2909: https://github.com/pydata/pandas/issues/2909
.. _GH2931: https://github.com/pydata/pandas/issues/2931
.. _GH2973: https://github.com/pydata/pandas/issues/2973
.. _GH2967: https://github.com/pydata/pandas/issues/2967


pandas 0.10.1
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,16 +2092,17 @@ def argsort(self, axis=0, kind='quicksort', order=None):

Returns
-------
argsorted : Series
argsorted : Series, with -1 indicated where nan values are present

"""
values = self.values
mask = isnull(values)

if mask.any():
result = values.copy()
result = Series(-1,index=self.index,name=self.name)
notmask = -mask
result[notmask] = np.argsort(values[notmask], kind=kind)
return Series(result, index=self.index, name=self.name)
result.values[notmask] = np.argsort(self.values[notmask], kind=kind)
return result
else:
return Series(np.argsort(values, kind=kind), index=self.index,
name=self.name)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,21 @@ def test_argsort(self):
argsorted = self.ts.argsort()
self.assert_(issubclass(argsorted.dtype.type, np.integer))

# GH 2967 (introduced bug in 0.11-dev I think)
s = Series([Timestamp('201301%02d'% (i+1)) for i in range(5)])
self.assert_(s.dtype == 'datetime64[ns]')
shifted = s.shift(-1)
self.assert_(shifted.dtype == 'datetime64[ns]')
self.assert_(isnull(shifted[4]) == True)

result = s.argsort()
expected = Series(range(5),dtype='int64')
assert_series_equal(result,expected)

result = shifted.argsort()
expected = Series(range(4) + [-1],dtype='int64')
assert_series_equal(result,expected)

def test_argsort_stable(self):
s = Series(np.random.randint(0, 100, size=10000))
mindexer = s.argsort(kind='mergesort')
Expand Down