在 Jupyter Notebook 中将数字转换为 DateTime

Converting number to DateTime in Jupyter Notebook

我有从 R 中的 CSV 导入的数据。日期时间采用 excel 格式(即 44410.095193、44410.095203 等)。

这是我的一小部分数据的复制品:

> dput(tail(raw.data[,1]))
structure(list(DateTime = c(44410.095193, 44410.095203, 44410.095215, 
44410.095227, 44410.095238, NA)), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x000002048fb61ef0>)

我发现有几种简单的方法可以将其转换为不同的格式(即 2021-08-02 02:17:04、2021-08-02 02:17:05 等) 通过使用 DescTools 包中的 XLDateToPOSIXctconvertToDateTime 形成桌面版 R 上的 openxlsx 包。

然而,这些功能在 R 的 Jupyter Notebook 版本中不起作用。我尝试在 Jupyter Notebook 中安装上面列出的软件包,但之后我总是收到错误消息:

#for the openxlsx package
Error in convertToDateTime(raw.data$DateTime): could not find function "convertToDateTime"
Traceback:

#for the DescTools package
Error in XLDateToPOSIXct(raw.data$DateTime): could not find function "XLDateToPOSIXct"
Traceback:

我认为必须有另一种方法来进行转换。我试过 as.Dateas.POSIXct 函数,但这些函数并没有提供我需要的所有数据(只是日期而不是时间)或者不正确(所有年份都是 1899 年)。

有哪些函数可以在 Jupyter Notebook 中使用,将 DateTime 的数字版本转换为实际的 date/time(例如,2021-08-02 02:17:05)?

A base R 解决方案应该适用于您的笔记本电脑。第二个 as.POSIXct 启用时间部分。确保您的 data.frame 日期是 numeric

dat <- c(44410.095193, 44410.095203, 44410.095215, 44410.095227, 44410.095238, NA)

class(dat)
[1] "numeric"

as.POSIXct( as.Date( dat, origin="1899-12-30" ) )
[1] "2021-08-02 04:17:04 CEST" "2021-08-02 04:17:05 CEST"
[3] "2021-08-02 04:17:06 CEST" "2021-08-02 04:17:07 CEST"
[5] "2021-08-02 04:17:08 CEST" NA