HYRY解决方案是对的,但是pandas不知道怎么处理
使用最新的 pandas 0.11-dev,timedeltas 现在得到全面支持
http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-deltas
In [25]: a = np.random.rand(8)*10
In [26]: a.sort()
In [27]: a
Out[27]:
array([ 0.72062151, 1.02039858, 2.07877837, 3.94256869, 5.5139672 ,
6.80194715, 6.83050498, 8.63027672])
# trick is to pass a nanosecond value directly
# pandas keeps all values internally as timedelta64[ns]
In [5]: pd.Series((np.round(a*1000)/1000)*1e9,dtype='timedelta64[ns]')
Out[5]:
0 00:00:00.721000
1 00:00:01.020000
2 00:00:02.079000
3 00:00:03.943000
4 00:00:05.514000
5 00:00:06.802000
6 00:00:06.831000
7 00:00:08.630000
dtype: timedelta64[ns]
如果你需要这个作为时间戳
In [8]: pd.Series((np.round(a*1000)/1000)*1e9,dtype='timedelta64[ns]') + pd.Timestamp('20110406')
Out[8]:
0 2011-04-06 00:00:00.721000
1 2011-04-06 00:00:01.020000
2 2011-04-06 00:00:02.079000
3 2011-04-06 00:00:03.943000
4 2011-04-06 00:00:05.514000
5 2011-04-06 00:00:06.802000
6 2011-04-06 00:00:06.831000
7 2011-04-06 00:00:08.630000
dtype: datetime64[ns]