如何重新采样timedelta? [英] How to resample timedeltas?

查看:71
本文介绍了如何重新采样timedelta?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在进行一项实验,该实验输出带有两列的数据:

I have been running an experiment that outputs data with two columns:

    自实验开始以来的
  1. 秒(浮动)
  2. 一种度量. (浮动)
  1. seconds since start of experiment (float)
  2. a measurement. (float)

我现在想将其加载到Pandas中以重新采样并绘制测量结果.我之前已经做过,但是那些时候我的时间戳记是从纪元开始或以日期时间(YYY-MM-DD HH:mm:ss)格式开始的.如果我将第一列加载为整数,我将无法执行

I would now like to load this into Pandas to resample and plot the measurements. I've done this before, but those times my timestamps have been since epoch or in datetime (YYY-MM-DD HH:mm:ss) format. If I'm loading my first column as integers I'm unable to do

data.resample('5Min', how='mean')

.如果将第一列转换为timedelta(seconds=...),这似乎也是不可能的.我的问题是,是否可以在不破坏时代转换的情况下重新采样这些数据?

. It also does not seem possible if I'd convert my first column to timedelta(seconds=...). My question is, is it possible to resample this data without subverting to epoch conversion?

推荐答案

您可以将groupbytime // period结合使用来进行此操作:

You can use groupby with time // period to do this:

import pandas as pd
import numpy as np

t = np.random.rand(10000)*3600
t.sort()
v = np.random.rand(10000)

df = pd.DataFrame({"time":t, "value":v})

period = 5*60
s = df.groupby(df.time // period).value.mean()
s.index *= period

这篇关于如何重新采样timedelta?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆