使用 strftime 从 Posixct 对象中提取日期和小时

Extracting date and hour from Posixct object with strftime

我试图使用 strftime 从日期时间列中提取日期和小时,但我不明白为什么返回的值比应返回的值早 1 小时。例如,对于 2013-01-01 00:00:00 的日期时间,返回的值应该是 2013-01-01 00 但我得到的是 2012-12-31 23。我也尝试添加 1 小时和然后提取但是在很长的日期序列中,它再次扰乱了输出。请参阅此示例代码以供参考。

## creating the sequence of time steps for cleaned
start <- as.POSIXct('2013-01-01 00:00:00',tz='EST')
end <- as.POSIXct('2016-06-06 23:00:00',tz='EST')

timesteps = data.frame( seq.POSIXt(from = start, to =end , by = "5 min"))
colnames(timesteps) = "Time Index"

dateandhour = function (timeindex){
return(strftime(timeindex, format = "%Y-%m-%d %H"))
}

timesteps ['Date and Hour'] = sapply(timesteps$`Time Index`, dateandhour)

请让我知道我在这里缺少什么。非常感谢。

那是因为您在 as.POSIXct 中指定了时区,而不是在 strptime 中。

timesteps[1,1]
[1] "2013-01-01 EST"

 strftime(timesteps[1,1], format = "%Y-%m-%d %H")
[1] "2012-12-31 21"
 strftime(timesteps[1,1], format = "%Y-%m-%d %H",tz='EST')
[1] "2013-01-01 00"`

dateandhour = function (timeindex){
  return(strftime(timeindex, format = "%Y-%m-%d %H",tz='EST'))
}

timesteps ['Date and Hour'] = sapply(timesteps$`Time Index`, dateandhour)

head(timesteps)
           Time Index Date and Hour
1 2013-01-01 00:00:00 2013-01-01 00
2 2013-01-01 00:05:00 2013-01-01 00
3 2013-01-01 00:10:00 2013-01-01 00
4 2013-01-01 00:15:00 2013-01-01 00
5 2013-01-01 00:20:00 2013-01-01 00
6 2013-01-01 00:25:00 2013-01-01 00

这里有两行答案:

创建序列

df <- data.frame(TimeIndex = 
         seq(anytime("2013-01-01 00:00:00"), 
             anytime("2016-06-06 23:00:00"), by="5 min"))

这创建了 361k 个观测值:

R> dim(df)
[1] 360841      1
R> 

转换为日期和小时

您可以在一次操作中完成此操作,因为 R 是矢量化的:

df$DateAndHour <- strftime(df$TimeIndex, "%Y-%m-%d %H")

我们可以检查:

R> head(df, 10)
             TimeIndex   DateAndHour
1  2013-01-01 00:00:00 2013-01-01 00
2  2013-01-01 00:05:00 2013-01-01 00
3  2013-01-01 00:10:00 2013-01-01 00
4  2013-01-01 00:15:00 2013-01-01 00
5  2013-01-01 00:20:00 2013-01-01 00
6  2013-01-01 00:25:00 2013-01-01 00
7  2013-01-01 00:30:00 2013-01-01 00
8  2013-01-01 00:35:00 2013-01-01 00
9  2013-01-01 00:40:00 2013-01-01 00
10 2013-01-01 00:45:00 2013-01-01 00
R> 

我使用上面的 anytime 是因为我觉得它简洁方便 -- 不需要格式。我们也可以使用 as.POSIXct()strptime()

我还省略了 tz 参数,以便一切都在我当地的时区。您可以在每次调用 anytime()strftime() 时设置它,或者设置 TZ 环境变量。

编辑: 由于 OP 询问性能,这里有一个快速比较。我需要稍微改变解决方案:

df <- data.frame(TimeIndex = seq(anytime("2013-01-01 00:00:00"),
                                 anytime("2016-06-06 23:00:00"), by="5 min"))

dateandhour <- function (timeindex) {
    return(strftime(timeindex, format = "%Y-%m-%d %H"))
}

f1 <- function(df) { data.frame(TimeIndex=df, DateAndHour=sapply(df, dateandhour)) }
f2 <- function(df) { data.frame(TimeIndex=df, DateAndHour=strftime(df$TimeIndex, "%Y-%m-%d %H")) }

library(rbenchmark)

benchmark(f1(df), f2(df), replications=10)[,1:4]

这样我就明白了:

R> benchmark(f1(df), f2(df), replications=10)[,1:4]
    test replications elapsed relative
1 f1(df)           10   7.101     2.08
2 f2(df)           10   3.414     1.00
R> 

大约两倍的改进。