tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.28k stars 272 forks source link

map_vec not returning same values as map_ for dates with timezones #1112

Closed PaulGunsalus closed 12 months ago

PaulGunsalus commented 12 months ago

purrr::map_vec is not retuning the same output as purrr::map when converting time zones.

tz <- c('America/Los_Angeles',  'America/Denver',  'America/Chicago',  'America/New_York')
dt <- as.POSIXct("13-06-2017 12:00:00 EDT", format = "%d-%m-%Y %H:%M:%S")
purrr::map_vec(tz, ~lubridate::with_tz(dt, .x))
purrr::map(tz, ~lubridate::with_tz(dt, .x))

## I would expect the object from map_vec to return something like this
v1 <- c(as.POSIXct('2017-06-13 09:00:00 PDT'), as.POSIXct('2017-06-13 10:00:00 MDT'), as.POSIXct('2017-06-13 11:00:00 CDT'), as.POSIXct('2017-06-13 12:00:00 EDT'))

This also does the same thing.

 purrr::map(tz, ~lubridate::with_tz(dt, .x)) |> purrr::list_c()
jennybc commented 12 months ago

The timezone for a POSIXct vector is a single attribute that applies to the whole object. The timezone is not something that can vary for element 1 vs, say, element 3. Since map() returns a list of length-1 POSIXct here, each one can have its own timezone. But when you force them all to live together in a POSIXct vector, such as the return value of map_vec() or list_c(), there must be a common timezone attribute.

PaulGunsalus commented 12 months ago

Thanks for clarifying, that makes sense.

I will close the thread.