0

I have a csv with entries

unique_id,creation_time
52,2021-08-09 18:55:43
53,2021-08-09 19:55:43
54,2021-08-09 20:55:43

After doing a fread(fread('mycsv.csv')), I get a DT ->

    unique_id        creation_time
  1       52  2021-08-09 18:55:43
  2       53  2021-08-09 19:55:43
  3       54  2021-08-09 20:55:43

When I do a str on the DT obtained, I see -

 ❯ str(dt)
Classes ‘data.table’ and 'data.frame':  <> obs. of  2 variables:
 $ unique_id                 : int  1 2 3
 $ creation_time      : POSIXct, format: "2021-08-09 18:55:43" "2021-08-09 19:55:43" "2021-08-09 20:55:43"

 

My first question is this -

How did fread know that my creation_time col is a timestamp. Why did it automatically convert it to a POSIXct ?

❯ dt[unique_id == 52][1]$creation_time
[1] "2021-08-09 18:55:43 UTC"

By default, this is converted to UTC. When I try to convert the tz to 'Asia/Kolkata' (this is taken from Sys.timezone()), nothing happens.

❯ Sys.timezone()
[1] "Asia/Kolkata"


❯ as.POSIXct(dt[unique_id == 52][1]$creation_time, tz='Asia/Kolkata')
[1] "2021-08-09 18:55:43 UTC"

It remains in UTC. However if I convert the timestamp back to a character type and then do a POSIXct with tz, it works

❯ as.POSIXct(as.character(dt[unique_id == 52][1]$creation_time), tz='Asia/Kolkata')
[1] "2021-08-09 18:55:43 IST"

Does POSIXct timezone conversion not happen on a POSIXct type ? Should it always be done on a character type ?

How does one change a timezone of an existing POSIXct variable ?

leoOrion
  • 1,833
  • 2
  • 26
  • 52
  • For "How did fread know that my creation_time col is a timestamp. Why did it automatically convert it to a POSIXct ?", wouldn't a natural first step be to study `?fread`, e.g. the second sentence? A more thorough description is found in the [NEWS](https://github.com/Rdatatable/data.table/blob/master/NEWS.md#potentially-breaking-changes-1) – Henrik Sep 24 '21 at 11:09
  • my main question and what i am trying to debug is the second part where the timezone is not changing. the fread question was some thing that popped up when I was trying to figure out how the UTC timezone was set in the first place. – leoOrion Sep 24 '21 at 11:52

0 Answers0