问题
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