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 70 forks source link

Merging zero-width objects removes the index #379

Closed joshuaulrich closed 1 year ago

joshuaulrich commented 1 year ago

merge.xts() needs to handle this the same as merge.zoo(). We also need to handle the cases where join is "left" or "right".

# zoo handles this correctly
#
z1 <- structure(numeric(0),
  index = structure(1:10, class = "Date"), class = "zoo")
z2 <- structure(numeric(0),
  index = structure(5:14, class = "Date"), class = "zoo")

merge(z1, z2)  # all index values
## Data:
## numeric(0)
## 
## Index:
##  [1] "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05" "1970-01-06" "1970-01-07" "1970-01-08"
##  [8] "1970-01-09" "1970-01-10" "1970-01-11" "1970-01-12" "1970-01-13" "1970-01-14" "1970-01-15"

merge(z1, z2, all = FALSE)  # only values in both objects
## Data:
## numeric(0)
## 
## Index:
## [1] "1970-01-06" "1970-01-07" "1970-01-08" "1970-01-09" "1970-01-10" "1970-01-11"

# xts, however...
merge(as.xts(z1), as.xts(z2))
## Data:
## numeric(0)
## 
## Index:
## POSIXct of length 0

merge(as.xts(z1), as.xts(z2), all = FALSE)
## Data:
## numeric(0)
## 
## Index:
## POSIXct of length 0

### Session Info
sessionInfo()                                                                                   
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
##  [4] 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              
## [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
## [1] xts_0.12.2.1 zoo_1.8-11  
## 
## loaded via a namespace (and not attached):
## [1] compiler_4.2.1  bspm_0.3.10     grid_4.2.1      lattice_0.20-45
joshuaulrich commented 1 year ago

Whoops, this is a duplicate of #227.