Skip to content

write_dta POSIXct dates silently dropped #634

Closed

Description

I had a data.frame with dates stored as POSIXct, and I wrote that as a Stata file using write_dta. In the resulting .dta file all of the dates were blank. I found that this doesn't happen to Date types, so I could convert them and it would work, but figured this warranted reporting.

EDIT to show this affects POSIXlt as well, but now I notice these are also being encoded as POSIXct, I don't know why.

library(haven)

test_data_out <- data.frame(my_POSIXct_date = as.POSIXct(c('2016-06-13',
                                                           '2015-08-17',
                                                           '2014-11-03',
                                                           '2012-10-01',
                                                           '2016-11-08')),
                            my_POSIXlt_date = as.POSIXlt(c('2016-06-13',
                                                           '2015-08-17',
                                                           '2014-11-03',
                                                           '2012-10-01',
                                                           '2016-11-08')),
                            my_Date_date = as.Date(c('2016-06-13',
                                                     '2015-08-17',
                                                     '2014-11-03',
                                                     '2012-10-01',
                                                     '2016-11-08')))
> str(test_data_out)
'data.frame':	5 obs. of  3 variables:
 $ my_POSIXct_date: POSIXct, format: "2016-06-13" ...
 $ my_POSIXlt_date: POSIXct, format: "2016-06-13" ...
 $ my_Date_date   : Date, format: "2016-06-13" ...
write_dta(test_data_out, 'test_data.dta')

test_data_in <- read_dta('test_data.dta')
> str(test_data_in)
tibble [5 × 3] (S3: tbl_df/tbl/data.frame)
 $ my_POSIXct_date: POSIXct[1:5], format: NA ...
 $ my_POSIXlt_date: POSIXct[1:5], format: NA ...
 $ my_Date_date   : Date[1:5], format: "2016-06-13" ...
> test_data_in$my_POSIXct_date
[1] NA NA NA NA NA
> test_data_in$my_POSIXlt_date
[1] NA NA NA NA NA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions