使用.unique()时Pandas切换时间戳

时间:2014-06-20 22:02:17

标签: python pandas

所以我想知道以下序列在熊猫中发生了什么,以及我如何解决它?:

In [88]: d = {1:{'idx':pd.Timestamp('2014-06-16 03:49:23.088652')}}

In [89]: x = pd.DataFrame.from_dict(d,orient='index')

In [90]: x
Out[90]:
                         idx
1 2014-06-16 03:49:23.088652

[1 rows x 1 columns]

In [91]: x.idx.unique()
Out[91]: array([1970-01-17 59:49:23.088652], dtype=datetime64[ns])

In [92]: pd.__version__
Out[92]: '0.13.1'

我遇到的问题来自于尝试这样的事情:

In [93]: idxs = x.idx.unique()

In [94]: idxs[0]-pd.tseries.offsets.Milli(40)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/cschwalbach/as_research_repo/logs/<ipython-input-94-914fa6d4ff6a> in <module>()
----> 1 idxs[0]-pd.tseries.offsets.Milli(40)

TypeError: ufunc 'subtract' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'

1 个答案:

答案 0 :(得分:0)

你必须附加nanos(因为numpy datetime数组*不理解pandas偏移量):

In [11]: idxs[0] - pd.tseries.offsets.Milli(40).nanos
Out[11]: numpy.datetime64('2014-06-15T20:49:23.048652000-0700')

* unique方法返回一个numpy数组而不是一个pandas对象......

注意这应该“正常”使用(最近的)pandas对象(系列):

In [12]: x['idx'] - pd.tseries.offsets.Milli(40)
Out[12]:
1   2014-06-16 03:49:23.048652
Name: idx, dtype: datetime64[ns]