Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 252
Markus Avatar asked Feb 02 '26 07:02

Markus


1 Answers

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
like image 139
Nir Graham Avatar answered Feb 03 '26 21:02

Nir Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!