Skip to content

Commit

Permalink
Ignore row.date if as.zoo is called with order.by
Browse files Browse the repository at this point in the history
quantmod:::as.zoo.data.frame() was added in c2f9916, on 2007-07-01.
The zoo package did not have an as.zoo.data.frame() method until
2010-08-03.

The quantmod method always assumes rownames should be used for the
index of the zoo object, either by converting them to Date or using
them as raw characters. But the zoo method will use a plain integer
index and assume the data are sorted if the user doesn't explicitly
provide an order.by argument.

Fixes #168.
  • Loading branch information
joshuaulrich committed Jun 16, 2017
1 parent 19cd7b6 commit 28c1982
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/quantmod.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ function(x)
`as.zoo.data.frame`<-
function(x,row.date=TRUE,...)
{
# ignore row.date if order.by is specified
if(hasArg("order.by")) {
zoo(x,...)
}
#really need to test order - ???how?
if(row.date) {
else if(row.date) {
zoo(x,as.Date(rownames(x),origin='1970-01-01'),...)
}
else {
Expand Down
25 changes: 25 additions & 0 deletions tests/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Call as.zoo before quantmod is loaded and registers its S3 method
dc <- c("2015-01-01", "2016-01-01", "2017-01-01")
dd <- as.Date(dc)

f <- data.frame(a = 1:3)
r <- f
rownames(r) <- dc

zz.f.date <- zoo::as.zoo(f, order.by = dd)
zz.f.char <- zoo::as.zoo(f, order.by = dc)
zz.f <- zoo::as.zoo(f)

zz.r.date <- zoo::as.zoo(r, order.by = dd)
zz.r.char <- zoo::as.zoo(r, order.by = dc)
zz.r <- zoo::as.zoo(r)

library(quantmod)

### quantmod:::as.zoo.data.frame

# should be the same as zoo:::as.zoo.data.frame when order.by is provided
stopifnot(identical(zz.f.char, as.zoo(f, order.by = dc)))
stopifnot(identical(zz.f.date, as.zoo(f, order.by = dd)))
stopifnot(identical(zz.r.char, as.zoo(r, order.by = dc)))
stopifnot(identical(zz.r.date, as.zoo(r, order.by = dd)))

0 comments on commit 28c1982

Please sign in to comment.