如何创建缺少日期时间值的时间序列 [英] How to create time series with missing datetime values

查看:105
本文介绍了如何创建缺少日期时间值的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带数据的csv文件. 链接在此处. 2013年的时间序列的粒度为5分钟.但是,某些时间戳记缺少该值.

I have csv file with data. Link is here. Granularity of time series is 5 min for year 2013. However, values are missing for some time stamps.

我想创建一个间隔为5分钟的时间序列,对于丢失的时间戳记,其值为零.

I want to create a time series with 5 minute interval with value zero for time stamps which are missing.

请告知如何在Pandas或R

Please advise how to do this either in Pandas or R

推荐答案

这应该有效

# partial old data used for example
timedata<- read.table(header = TRUE, sep =",", text = "
timestamp, value
01/01/2013 00:00:10,10
01/01/2013 00:00:25,6
01/01/2013 00:00:40,10
01/01/2013 00:00:55,8
")
# for your old timestamp dataframe use: 
# colnames(olddata)<- c("timestamp", "value") to get a suitable header

# create full sequence of timestamps
filldata<-as.data.frame(format(seq(from=ISOdate(2013,1,1,hour=0),to=ISOdate(2013,1,1,hour=24), by="5 sec"), "%d/%m/%Y %H:%M:%S"))
colnames(filldata)<- "timestamp"

# merge and make NAs zero
filleddata<- merge(filldata,timedata, by="timestamp", all=TRUE)
filleddata$value[is.na(filleddata$value)]<- 0

这篇关于如何创建缺少日期时间值的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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