本文介绍了将 pandas 时间戳四舍五入到分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想基于pd_date_range()
的开始和结束时间戳记(从纪元起以微秒为单位)以1分钟为间隔创建一个DateTimeIndex
.为此,我需要将开始时间戳向上舍入,并将结束时间戳向下舍入.这是我到目前为止的内容:
I want to create a DateTimeIndex
at 1 minute intervals based on a start and end timestamp (given in microseconds since epoch) with pd_date_range()
. To do this, I need to round the starting timestamp up and the ending timestamp down. Here is what I have so far:
import pandas as pd
start = 1406507532491431
end = 1406535228420914
start_ts = pd.to_datetime(start, unit='us') # Timestamp('2014-07-28 00:32:12.491431')
end_ts = pd.to_datetime(end, unit='us') # Timestamp('2014-07-28 08:13:48.420914')
我想四舍五入:
start_ts
至Timestamp('2014-07-28 00:32')
和
end_ts
至Timestamp('2014-07-28 08:14')
.
我该怎么做?
推荐答案
以一种简单的方法执行此操作目前是一个未解决的问题此处
Doing this in a simple method is currently an outstanding issue here
In [22]: start = 1406507532491431
In [23]: end = 1406535228420914
[26]: dti = pd.to_datetime([start,end],unit='us')
In [27]: dti
Out[27]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-07-28 00:32:12.491431, 2014-07-28 08:13:48.420914]
Length: 2, Freq: None, Timezone: None
In [29]: pd.DatetimeIndex(((dti.asi8/(1e9*60)).round()*1e9*60).astype(np.int64))
Out[29]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-07-28 00:32:00, 2014-07-28 08:14:00]
Length: 2, Freq: None, Timezone: None
尽管如此,它还是很简单的.
Nevertheless its quite straightforward.
欢迎实施请求.
这篇关于将 pandas 时间戳四舍五入到分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!