How to create time series with missing datetime values

若如初见. 提交于 2019-12-08 10:12:13

问题


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.

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

Please advise how to do this either in Pandas or R


回答1:


This should work

# 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


来源:https://stackoverflow.com/questions/26820843/how-to-create-time-series-with-missing-datetime-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!