joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
219 stars 71 forks source link

Can't subset empty xts by date anymore #252

Closed alkment closed 6 years ago

alkment commented 6 years ago

Subsetting by date behavior changed in case of empty xts

After upgrading from 10-2 to 11-0 subsetting an empty xts by a date throws an error.

Expected behavior

The pre 11-0 behavior was to return an empty xts.

Minimal, reproducible example

library(xts)
testxts=xts(1,as.Date('2018-07-18'))
testxts['2017'][as.Date('2018-07-17')] # in 10-2 this works and returns an empty xts; in 11-0 this throws an error
#  testxts[as.Date('2018-07-17')]  this still works

Session Info

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  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] xts_0.11-0 zoo_1.8-3 

loaded via a namespace (and not attached):
[1] compiler_3.4.4  tools_3.4.4     yaml_2.1.19     grid_3.4.4      lattice_0.20-35
AndreMikulec commented 6 years ago

The error specifically is this.

> testxts['2017'][as.Date('2018-07-17')]
Error in if (j < 1 || idx[j] != dt) break :
  missing value where TRUE/FALSE needed
AndreMikulec commented 6 years ago

This may? fix the problem ( prevent NAs from creeping in )

from

> debug(window_idx)
> testxts['2017'][as.Date('2018-07-17')]
debug at <tmp>#23: match <- idx[base_idx] == index.

one may do

> fixInNamespace(x = "window_idx", ns = "xts")

# replace line
match <- idx[base_idx] == index.

# by lines

### match <- idx[base_idx] == index.
### prevent NAs from creeping in
match <- sapply({idx[base_idx] == index.}, function(x) { if(is.na(x)) { FALSE } else { x } } )

SAVE notepad

So, now works as exptected.

> testxts['2017'][as.Date('2018-07-17')]
     [,1]
> str(testxts['2017'][as.Date('2018-07-17')])
An 'xts' object of zero-width
> devtools::session_info()
Session info ------------------------------------------------------------------
 setting  value
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, mingw32
 ui       RTerm
 language (EN)
 collate  English_United States.1252
 tz       America/Chicago
 date     2018-07-22

Packages ----------------------------------------------------------------------
 package    * version date       source
 base       * 3.5.1   2018-07-02 local
 compiler     3.5.1   2018-07-02 local
 datasets   * 3.5.1   2018-07-02 local
 devtools     1.13.6  2018-06-27 CRAN (R 3.5.1)
 digest       0.6.15  2018-01-28 CRAN (R 3.5.0)
 graphics   * 3.5.1   2018-07-02 local
 grDevices  * 3.5.1   2018-07-02 local
 grid         3.5.1   2018-07-02 local
 lattice      0.20-35 2017-03-25 CRAN (R 3.5.0)
 magrittr   * 1.5     2014-11-22 CRAN (R 3.5.0)
 memoise      1.1.0   2017-04-21 CRAN (R 3.5.0)
 methods    * 3.5.1   2018-07-02 local
 rstudioapi   0.7     2017-09-07 CRAN (R 3.5.0)
 stats      * 3.5.1   2018-07-02 local
 tools        3.5.1   2018-07-02 local
 utils      * 3.5.1   2018-07-02 local
 withr        2.1.2   2018-04-27 Github (jimhester/withr@79d7b0d)
 xts        * 0.11-0  2018-07-16 CRAN (R 3.5.1)
 zoo        * 1.8-3   2018-07-16 CRAN (R 3.5.1)