BNL
BNL

Reputation: 63

XTS time subselect across DST date Linux vs Windows

On Windows the following subselect code produces an incorrect XTS object, but works correctly on my Ubuntu machine

library(xts)
theTimes <- seq(from=as.POSIXct('2016-03-10 12:00:00 CDT'),
              to=as.POSIXct('2016-03-20 12:10:00 CDT'),by=60)

ExampleData <- xts(rep(1,length(theTimes)),theTimes)
CutExampleData <- ExampleData['T02:00/T16:00']
any(duplicated(index(CutExampleData)))  ## Evaluates to TRUE on windows (incorrect) and FALSE on Ubuntu (correctly)

SessionInfo on Linux PC:

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] RMySQL_0.10.9 DBI_0.5-1     Quandl_2.8.0  xts_0.9-7     zoo_1.7-13   

loaded via a namespace (and not attached):
[1] httr_1.2.1      R6_2.1.3        tools_3.3.1     grid_3.3.1      jsonlite_1.0    lattice_0.20-33

SessionInfo on Windows PC:

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] xts_0.9-7  zoo_1.7-13

loaded via a namespace (and not attached):
[1] grid_3.3.2      lattice_0.20-34

I think this has to do with DST, since the index spans the recent switch over date. Any ideas?

Upvotes: 1

Views: 103

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176688

I was able to replicate this on my Windows machine. It looks like an infelicity in strptime and/or as.POSIXct.POSIXlt between *nix and Windows versions of R. The problem manifests because your start time is 02:00:00, which doesn't exist on 2016-03-13 because times go from 01:59:59.999 to 03:00:00 in the America/Chicago timezone due to daylight saving time.

A work-around is to set your start time to just before 02:00:00.

library(xts)
theTimes <- seq(from=as.POSIXct('2016-03-12 00:00:00', tz="America/Chicago"),
                to=as.POSIXct('2016-03-14 23:00:00', tz="America/Chicago"), by=60)
ExampleData <- xts(rep(1,length(theTimes)),theTimes)
# 01:59 instead of 02:00 to avoid DST issue
CutExampleData <- ExampleData['T01:59/T16:00']
anyDuplicated(index(ExampleData))
anyDuplicated(index(CutExampleData))  # 0 (no duplicates)

Also note that "CDT" is not a good way to specify a timezone in R. The three-letter timezone abbreviations (aside from "GMT" and "UTC") may be ambiguous, so it's better to use the Region/City specification.

Upvotes: 1

Related Questions