Skip to content

BUG: Series.argsort() fails with datetime64[ns] with NaT / dtypes are odd #2967

@jreback

Description

@jreback

pretty simple fix though, just need to make sure that the result array
is typed float64 (to accomodate the nans) or int64 (if no nans)

(rather than the same as input type)
(which could give weird results in some cases), e.g. you wouldn't
want a float array back just because you fed it nans....

heres the inspiration question
http://stackoverflow.com/questions/15207279/return-sorted-indexes-skipping-nan-values-in-pandas

In [29]: s = pd.Series([pd.Timestamp('201301%02d'% (i+1)) for i in range(5)])

In [30]: s
Out[30]: 
0   2013-01-01 00:00:00
1   2013-01-02 00:00:00
2   2013-01-03 00:00:00
3   2013-01-04 00:00:00
4   2013-01-05 00:00:00
dtype: datetime64[ns]

In [31]: s.argsort()
Out[31]: 
0    0
1    1
2    2
3    3
4    4
dtype: int64

In [32]: s.shift().argsort()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-61a586f08c06> in <module>()
----> 1 s.shift().argsort()

/mnt/home/jreback/pandas/pandas/core/series.pyc in argsort(self, axis, kind, order)
   2119             result = values.copy()
   2120             notmask = -mask
-> 2121             result[notmask] = np.argsort(values[notmask], kind=kind)
   2122             return Series(result, index=self.index, name=self.name)
   2123         else:

TypeError: array cannot be safely cast to required type

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions