python – 熊猫:时间戳索引到最接近的第5分钟
发布时间:2021-01-17 13:46:46 所属栏目:Python 来源:互联网
导读:我有一个df与通常的时间戳作为索引: 2011-04-01 09:30:00 2011-04-01 09:30:10 ... 2011-04-01 09:36:20 ... 2011-04-01 09:37:30 如何创建具有相同时间戳的数据帧的列,但四舍五入到最接近的第5分钟间隔?喜欢这个: index new_col
我有一个df与通常的时间戳作为索引: 2011-04-01 09:30:00 2011-04-01 09:30:10 ... 2011-04-01 09:36:20 ... 2011-04-01 09:37:30 如何创建具有相同时间戳的数据帧的列,但四舍五入到最接近的第5分钟间隔?喜欢这个: index new_col 2011-04-01 09:30:00 2011-04-01 09:35:00 2011-04-01 09:30:10 2011-04-01 09:35:00 2011-04-01 09:36:20 2011-04-01 09:40:00 2011-04-01 09:37:30 2011-04-01 09:40:00 解决方法Theround_to_5min(t) solution using timedelta arithmetic是正确的但复杂而且很慢.相反,在熊猫中使用漂亮的Timstamp:
import numpy as np import pandas as pd ns5min=5*60*1000000000 # 5 minutes in nanoseconds pd.to_datetime(((df.index.astype(np.int64) // ns5min + 1 ) * ns5min)) 我们来比较一下速度: rng = pd.date_range('1/1/2014','1/2/2014',freq='S') print len(rng) # 86401 # ipython %timeit %timeit pd.to_datetime(((rng.astype(np.int64) // ns5min + 1 ) * ns5min)) # 1000 loops,best of 3: 1.01 ms per loop %timeit rng.map(round_to_5min) # 1 loops,best of 3: 1.03 s per loop 只要约1000倍快! (编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – 如何串流叽叽叽t witter y y y?
- 设置python中命名参数的默认设置
- python – 基本的openGL,顶点缓冲区和pyglet
- Python语言中的函数range()没有给出预期的结果
- python – 查找numpy数组中每行的最大值以及相同大小的另一
- python – 在keras的LSTM中使用隐藏状态而不是输出
- 为什么python-cgi在unicode上失败?
- python – 为什么pow(x,y)的时间复杂度为O(1),而x ** y为O(
- 错误:输入’for’Python时没有可行的替代方案
- Python / Scipy – 将optimize.curve_fit的sigma实现到opti