如何舍弃 pandas DatetimeIndex? [英] How to round a Pandas `DatetimeIndex`?
问题描述
我有一个 pandas.DatetimeIndex
,例如:
pd .date_range('2012-1-1 02:03:04.000',periods = 3,freq ='1ms')
>>> [2012-01-01 02:03:04,...,2012-01-01 02:03:04.002000]
我想将日期( Timestamp
s)舍入到最接近的秒。我怎么做?预期结果类似于:
[2012-01-01 02:03:04.000000,...,2012-01 -01 02:03:04.000000]
有可能通过舍入一个Numpy datetime64 [ns]
到不更改 dtype
[ns]
np.array(['2012-01-02 00:00:00.001'],dtype ='datetime64 [ns] ')
更新:如果你这样做datetimeIndex / datetime64列更好的方法是直接使用 np.round
而不是通过apply / map:
np.round(dtindex_or_datetime_col.astype(np.int64),-9).astype('datetime64 [ns]')
旧答案(有一些更多解释):
虽然@ Matti的答案显然是正确的交易方式在你的情况下,我以为我会添加一个答案,你如何将一个时间戳到最接近的秒数/ p>
从pandas.lib import Timestamp
t1 =时间戳('2012-1-1 00:00 :00')
t2 =时间戳('2012-1-1 00:00:00.000333')
在[4]中:t1
输出[4]:<时间戳:2012-01-01 00:00:00>
在[5]中:t2
输出[5]:< Timestamp:2012-01-01 00:00:00.000333>
在[6]中:t2.microsecond
输出[6]:333
在[7]中:t1.value
输出[7] :1325376000000000000L
在[8]中:t2.value
输出[8]:1325376000000333000L
#或者:t2.value - t2.value%1000000000
在[9]中:long(round(t2.value,-9))#round milli-,micro-and nano-seconds
Out [9]:1325376000000000000L
在[ 10]:Timestamp(long(round(t2.value,-9)))
Out [10]:< Timestamp:2012-01-01 00:00:00>
因此,您可以将其应用于整个索引:
$ b $
返回时间戳(long(round(ts.value,-9)))
dtindex.map (to_the_second)
I have a pandas.DatetimeIndex
, e.g.:
pd.date_range('2012-1-1 02:03:04.000',periods=3,freq='1ms')
>>> [2012-01-01 02:03:04, ..., 2012-01-01 02:03:04.002000]
I would like to round the dates (Timestamp
s) to the nearest second. How do I do that? The expected result is similar to:
[2012-01-01 02:03:04.000000, ..., 2012-01-01 02:03:04.000000]
Is it possible to accomplish this by rounding a Numpy datetime64[ns]
to seconds without changing the dtype
[ns]
?
np.array(['2012-01-02 00:00:00.001'],dtype='datetime64[ns]')
Update: if you're doing this to a DatetimeIndex / datetime64 column a better way is to use np.round
directly rather than via an apply/map:
np.round(dtindex_or_datetime_col.astype(np.int64), -9).astype('datetime64[ns]')
Old answer (with some more explanation):
Whilst @Matti's answer is clearly the correct way to deal with your situation, I thought I would add an answer how you might round a Timestamp to the nearest second:
from pandas.lib import Timestamp
t1 = Timestamp('2012-1-1 00:00:00')
t2 = Timestamp('2012-1-1 00:00:00.000333')
In [4]: t1
Out[4]: <Timestamp: 2012-01-01 00:00:00>
In [5]: t2
Out[5]: <Timestamp: 2012-01-01 00:00:00.000333>
In [6]: t2.microsecond
Out[6]: 333
In [7]: t1.value
Out[7]: 1325376000000000000L
In [8]: t2.value
Out[8]: 1325376000000333000L
# Alternatively: t2.value - t2.value % 1000000000
In [9]: long(round(t2.value, -9)) # round milli-, micro- and nano-seconds
Out[9]: 1325376000000000000L
In [10]: Timestamp(long(round(t2.value, -9)))
Out[10]: <Timestamp: 2012-01-01 00:00:00>
Hence you can apply this to the entire index:
def to_the_second(ts):
return Timestamp(long(round(ts.value, -9)))
dtindex.map(to_the_second)
这篇关于如何舍弃 pandas DatetimeIndex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!