Question

fread column with only NA values as POSIXct returns warning and a character column

Update: I have opened an issue for this: https://github.com/Rdatatable/data.table/issues/6208


I would like to read a column of a NA values as POSIXct using fread.

I would expect this to work:

library(data.table)
fwrite(data.table(datetime = as.POSIXct(NA)), "test.csv")
fread("test.csv", colClasses = "POSIXct")

However, this gives the following warning:

Warning message:
Column 'datetime' was requested to be 'POSIXct' but fread encountered the following error:
    character string is not in a standard unambiguous format
so the column has been left as type 'character' 

In contrast using utils::write.csv and utils::read.csv this works:

write.csv(data.frame(datetime = as.POSIXct(NA)), "test.csv", row.names = FALSE)
read.csv("test.csv", colClasses = "POSIXct")

Is there a possible workaround?

I used data.table_1.15.4.

 4  60  4
1 Jan 1970

Solution

 1

a work around is to have fread always read it as double/numeric, and convert to posixct after.

library(data.table)
fwrite(data.table(datetime = as.POSIXct(NA)), "test.csv")
dt_in <- fread("test.csv", colClasses = "double")[, datetime := as.POSIXct(datetime, origin="1970-01-01")]
dt_in
2024-06-27
Nir Graham