na.approx changing timezone display
I recently found the zoo function na.approx but was wondering if there was a way to be able to use it without it altering the way that timezones are displayed, as shown if running my reproducible example...
set.seed(111) x <- xts(matrix(cumprod(rnorm(100,0,0.001)+1)*100, ncol=1,dimnames=list(rep("",100),c("JJ"))), Sys.time()-c(100:1), tzone="America/Chicago") x[30:50,] <- NA tzone(x) [1] "America/Chicago" .index(head(x,1)) [1] 1377270598 attr(,"tzone") [1] "America/Chicago" attr(,"tclass") [1] "POSIXct" "POSIXt" head(x) JJ 2013-08-23 10:09:57 100.02352 2013-08-23 10:09:58 99.99044 2013-08-23 10:09:59 99.95928 2013-08-23 10:10:00 99.72914 2013-08-23 10:10:01 99.71210 2013-08-23 10:10:02 99.72609 Warning message: timezone of object (America/Chicago) is different than current timezone (). y <- na.approx(x) tzone(y) [1] "America/Chicago" .index(head(y,1)) [1] 1377270598 head(y) JJ 2013-08-23 16:09:57 100.02352 2013-08-23 16:09:58 99.99044 2013-08-23 16:09:59 99.95928 2013-08-23 16:10:00 99.72914 2013-08-23 16:10:01 99.71210 2013-08-23 16:10:02 99.72609 Warning message: timezone of object (America/Chicago) is different than current timezone ().
N.B. I am not based in Chicago....and my Sys.getenv('TZ') is not set to "America/Chicago"
Answers
Edit: I could reproduce the issue with xts 0.9-3. It has been fixed in xts 0.9-5. You need to update xts.
I believe this could be considered a bug.
head(x) # JJ #2013-08-23 14:12:19 100.02352 #2013-08-23 14:12:20 99.99044 #2013-08-23 14:12:21 99.95928 #2013-08-23 14:12:22 99.72914 #2013-08-23 14:12:23 99.71210 #2013-08-23 14:12:24 99.72609 methods(na.approx) #[1] na.approx.default na.approx.ts* na.approx.zoo* na.approx.zooreg*
So, there is no xts method. Since
class(x) #[1] "xts" "zoo"
na.approx.zoo is dispatched. Inside that function essentially this happens:
index(x) <- index(x) head(x) # JJ #2013-08-23 21:12:19 100.02352 #2013-08-23 21:12:20 99.99044 #2013-08-23 21:12:21 99.95928 #2013-08-23 21:12:22 99.72914 #2013-08-23 21:12:23 99.71210 #2013-08-23 21:12:24 99.72609
Note that index<-.xts exists and is used here. Unfortunately, contrary to index<-.zoo, it doesn't preserve the time zone (of the index).
So, you could use y <- na.approx(as.zoo(x)) to avoid the issue.