从 tibble 数据帧转换时区

问题描述 投票:0回答:1

我正在从 alphavantager R 包下载 tibble 数据帧。我正在尝试将时间戳列从默认东部时区转换为 UTC。我的所有尝试都保持时间戳不变,除了在某些尝试中在末尾添加后缀“UTC”。

library('alphavantager')

tst <- av_get(symbol         = "NKE",
              av_fun         = "TIME_SERIES_INTRADAY",
              interval       = "15min",
              extended_hours = "false",
              month          = "2024-03",
              outputsize     = "full") 

# parsing out 1 day  
tst<-tst[as.Date(tst$timestamp)=='2024-03-01',]

# attempt #1
tst$timestamp <- as.POSIXct(as.integer(tst$timestamp),tz="UTC")

# attempt #2
tst$timestamp <- as.POSIXct(as.integer(tst$timestamp), origin="1970-01-01", tz="UTC")

# attempt #3
tst<-as.data.frame(tst)

tst$timestamp <- as.POSIXct(as.integer(tst$timestamp),tz="UTC")

# attempt #4
tst <- as.list(tst)

tst$timestamp <- as.POSIXct(as.integer(tst$timestamp),tz="UTC")`

# example data
dput(head(tst$timestamp))

# Output of dput
# structure(c(1709285400, 1709286300, 1709287200, 1709288100, 
# 1709289000,1709289900), class = c("POSIXct", "POSIXt"), tzone = 
#"UTC")

r timezone tibble
1个回答
0
投票

它对我来说下载为

tz = UTC
,我可以将其更改为
CET
。 (源 Alphavantager JSON 元数据显示
EST
。)

library(alphavantager)
library(lubridate)

av_api_key("xxx")

tst <- av_get(
  symbol = "NKE",
  av_fun = "TIME_SERIES_INTRADAY",
  interval = "15min",
  extended_hours = "false",
  month = "2024-03",
  outputsize = "full"
)

# Downloaded as UTC
tz(tst$timestamp)
#> [1] "UTC"
tst$timestamp[1]
#> [1] "2024-03-01 09:30:00 UTC"

# Change to CET
tz(tst$timestamp) <- "CET"
tz(tst$timestamp)
#> [1] "CET"
tst$timestamp[1]
#> [1] "2024-03-01 09:30:00 CET"

# Equivalent to
x <- as_datetime("2024-03-01 09:30:00 UTC")
x
#> [1] "2024-03-01 09:30:00 UTC"
tz(x) <- "CET"
x
#> [1] "2024-03-01 09:30:00 CET"

# Metadata
"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=NKE&interval=5min&apikey=xxx"
#> [1] "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=NKE&interval=5min&apikey=xxx"

创建于 2024-04-25,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.