Yar*_*riv 13 datetime numpy date date-format pandas
我有一个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]
Run Code Online (Sandbox Code Playgroud)
我想将日期舍入Timestamp
到最接近的秒.我怎么做?预期结果类似于:
[2012-01-01 02:03:04.000000, ..., 2012-01-01 02:03:04.000000]
Run Code Online (Sandbox Code Playgroud)
是否有可能通过将Numpy四舍五入datetime64[ns]
到几秒而不改变dtype
[ns]
?
np.array(['2012-01-02 00:00:00.001'],dtype='datetime64[ns]')
Run Code Online (Sandbox Code Playgroud)
And*_*den 16
更新:如果您对DatetimeIndex/datetime64列执行此操作,更好的方法是np.round
直接使用而不是通过apply/map:
np.round(dtindex_or_datetime_col.astype(np.int64), -9).astype('datetime64[ns]')
Run Code Online (Sandbox Code Playgroud)
旧答案(有更多解释):
虽然@Matti的答案显然是处理你的情况的正确方法,但我想我会添加一个答案,你可以如何将时间戳四舍五入到最接近的秒:
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>
Run Code Online (Sandbox Code Playgroud)
因此,您可以将其应用于整个索引:
def to_the_second(ts):
return Timestamp(long(round(ts.value, -9)))
dtindex.map(to_the_second)
Run Code Online (Sandbox Code Playgroud)
wom*_*ire 13
round()
对于DatetimeIndex,时间戳,和TimedeltaIndex在Timedelta大熊猫0.18.0加入方法.现在我们可以做到以下几点:
In[114]: index = pd.DatetimeIndex([pd.Timestamp('2012-01-01 02:03:04.000'), pd.Timestamp('2012-01-01 02:03:04.002'), pd.Timestamp('20130712 02:03:04.500'), pd.Timestamp('2012-01-01 02:03:04.501')])
In[115]: index.values
Out[115]:
array(['2012-01-01T02:03:04.000000000', '2012-01-01T02:03:04.002000000',
'2013-07-12T02:03:04.500000000', '2012-01-01T02:03:04.501000000'], dtype='datetime64[ns]')
In[116]: index.round('S')
Out[116]:
DatetimeIndex(['2012-01-01 02:03:04', '2012-01-01 02:03:04',
'2013-07-12 02:03:04', '2012-01-01 02:03:05'],
dtype='datetime64[ns]', freq=None)
Run Code Online (Sandbox Code Playgroud)
round()
接受频率参数.此处列出了它的字符串别名.
归档时间: |
|
查看次数: |
11182 次 |
最近记录: |