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

xts() contructor creates list coredata when 'x' is a zero-row data.frame #394

Closed joshuaulrich closed 1 year ago

joshuaulrich commented 1 year ago

xts objects should always have (atomic) vector/matrix coredata. List coredata is not supported, but the xts() constructor allows you to create an object with list coredata when the input is a zero-row data.frame. The constructor should instead call as.matrix() on the data.frame.

library(xts)
zero_row_df <- data.frame(date = .Date(numeric(0)), x = numeric(0), y = numeric(0))
zero_row_xts <- xts(zero_row_df[, -1], zero_row_df[, 1])

str(zero_row_xts)
## An empty xts object 
##   Data:    list [2, 0]           # <- list!
##   Index:   Date [0] (TZ: "UTC")
##   xts Attributes:
##     $ row.names: int(0)