将 Pandas 时间戳舍入为分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27031169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding Pandas Timestamp to minutes
提问by mchangun
I want to create a DateTimeIndexat 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:
我想DateTimeIndex根据开始和结束时间戳(自纪元以来以微秒为单位)以pd_date_range(). 为此,我需要将起始时间戳向上舍入,将结束时间戳向下舍入。这是我到目前为止所拥有的:
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')
I want to round:
我想圆:
start_tsto Timestamp('2014-07-28 00:32')and
start_ts以Timestamp('2014-07-28 00:32')和
end_tsto Timestamp('2014-07-28 08:14').
end_ts到Timestamp('2014-07-28 08:14')。
How can I do this?
我怎样才能做到这一点?
采纳答案by Jeff
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.
尽管如此,它还是很简单的。
Pull-requests to implement are welcome.
欢迎提出执行请求。
回答by Gustavo Bezerra
As of version 0.18, Pandas has built-in datetime-like rounding functionality:
从 0.18 版本开始,Pandas 具有内置的类似日期时间的舍入功能:
start_ts.round('min') # Timestamp('2014-07-28 00:32:00')
end_ts.round('min') # Timestamp('2014-07-28 08:14:00')
You can also use .ceilor .floorif you need to force the rounding up or down.
如果您需要强制向上或向下舍入,您也可以使用.ceilor .floor。
EDIT:
The above code works with raw pd.Timestamp, as asked by the OP. In case you are working with a pd.Series, use the dtaccessor:
编辑:上面的代码适用于 raw pd.Timestamp,正如 OP 所要求的那样。如果您正在使用 a pd.Series,请使用dt访问器:
s = pd.Series(pd.to_datetime([1406507532491431000, 1406535228420914000]))
s.dt.round('min')
Output:
输出:
0 2014-07-28 00:32:00
1 2014-07-28 08:14:00
dtype: datetime64[ns]
回答by user3735204
I had a similar problem, wanting to round off to the day. Turns out there's an easy way (it works for Y[ear] M[month] D[ay], h[our], m[inute], s[econd]). Assuming df is a pandas DataFrame with a column 'datecol':
我有一个类似的问题,想要四舍五入到今天。结果证明有一个简单的方法(它适用于 Y[ear] M[month] D[ay]、h[our]、m[inute]、s[econd])。假设 df 是一个带有“datecol”列的 Pandas DataFrame:
df['datecol'] = df['datecol'].values.astype('<M8[m]')
Will round it off to the m[inute]. Given that I found this question originally, I thought I'd link back the answer I got as it seems relevant,
将其四舍五入到 m[inute]。鉴于我最初发现了这个问题,我想我会链接回我得到的答案,因为它似乎相关,
回答by aitorhh
As @user3735204 stated, it is possible to round off a columns with:
正如@user3735204 所说,可以使用以下方法对列进行四舍五入:
df['datecol'] = df['datecol'].astype('datetime64[m]')
where the unit in the square brackets could be:
方括号中的单位可以是:
Y[ear] M[month] D[ay], h[our], m[inute], s[econd]
It is also possible to round to the nearest (reference) by making the column as index and applying the roundmethod (available at pandas 0.19.0):
还可以通过将列作为索引并应用舍入方法(在 pandas 0.19.0 中可用)来舍入到最近的(参考):
df.index = pd.to_datetime(df['datecol'])
df.index = df.index.round("S")
Example:
例子:
df = pd.DataFrame(data = tmpdata)
df['datecol'] = df['datecol'].astype('datetime64[s]')
print df['datecol']
0 2016-10-05 05:37:42
1 2016-10-05 05:37:43
Name: datecol, dtype: datetime64[ns]
df.index = pd.to_datetime(df['datecol'])
df.index = df.index.round("S")
print df.index
DatetimeIndex(['2016-10-05 05:37:43', '2016-10-05 05:37:43'], dtype='datetime64[ns]', name=u'timestamp', freq=None)
回答by Kirubaharan J
import pandas as pd
new_index = pd.date_range(start=start_ts.strftime('%Y-%m-%d %H:%M'), end=end_ts.strftime('%Y-%m-%d %H:%M'), freq='1min')

