Skip to content

[Bug] CJ() looses timezone of POSIXct vector #2029

@MarkusBonsch

Description

@MarkusBonsch

I have a problem when using CJ() with POSIXct objects.

If CJ is used to construct a data.table from a POSIXct vector with timezone UTC and a second grouping vector, the resulting POSIXct column in the data.table has local timezone.
If the POSIXct vector is the only input to CJ, the timezone is preserved.
Also, plain data.table() preserves the timezone.

See below for a reproducible example and my sessionInfo(). Maybe related to (#1778)

library(data.table)

## get groups and dates as input for CJ()
input_groups <- letters[1:3]
input_times  <- as.POSIXct(sprintf("2016-%02d-01", 1:12), tz = "UTC")

## input_times timezone is UTC
all(lapply(input_times, attr, "tzone") == "UTC")
# [1] TRUE

## the following shows that CJ changes timezones to NULL (local time).
test <- CJ(group = input_groups, test_time = input_times)
## test$test_time timezone is not UTC but NULL
all(lapply(test$test_time, attr, "tzone") == "UTC")
# [1] FALSE
unique(lapply(test$test_time, attr, "tzone"))
# [[1]]
# NULL

## if only a single POSIXct vector is supplied, timezone is preserved.
test2 <- CJ(test_time = input_times)
all(lapply(test2$test_time, attr, "tzone") == "UTC")
# [1] TRUE

## also, original data.table() behaves as expected.
test2 <- data.table(group = rep(input_groups, length(input_times)), date = rep(input_times, length(input_groups)))
setkey(test2, group, date)
all(lapply(test2$date, attr, "tzone") == "UTC")
# [1] TRUE

Here is my sessionInfo()

R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.10.4

loaded via a namespace (and not attached):
[1] tools_3.3.0

Kind regards,
Markus Bonsch

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions