Skip to content

Commit

Permalink
Convert index to Date using index timezone
Browse files Browse the repository at this point in the history
Both the indexClass<-.xts() call and the indexTZ<-.xts() calls set the
index timezone to UTC, but does not change the actual index values.
The call to 'index(x) <- index(x)' updates the values, but the dates
will calculated as if the local time is UTC, not the local time of the
user-specified xts object. This can cause the output to be different
than if the local timezone is used.

Convert the index to Date using the local timezone before updating the
index timezone.

Fixes #53, #277.
  • Loading branch information
joshuaulrich committed Nov 3, 2018
1 parent 7c2809f commit bfcbe50
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changed in xts 0.11-2:

o The to.period() family of functions now use the index timezone when
converting intraday index values to daily values (or lower frequency).
Thanks to Garrett See and Gabor Grothendieck for the reports (#53, #277).

o Make column names for merge() results with unnamed objects shorter and more
like zoo (#248). This also makes na.fill() much faster (#259).
BREAKING: This may break existing code for integer unnamed objects.
Expand Down
2 changes: 2 additions & 0 deletions R/toperiod.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ function(x) {
if(is.xts(x)) {
# if x is xts, drop HHMMSS from index
if(any(indexClass(x)=='POSIXt')) {
# convert index to Date
index(x) <- as.Date(index(x), tz = indexTZ(x))
indexClass(x) <- "Date" # set indexClass to Date
}
if(any(indexClass(x) %in% .classesWithoutTZ)) {
Expand Down
10 changes: 10 additions & 0 deletions inst/unitTests/runit.period.apply.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,13 @@ test.period.prod_converts_index_to_integer <- function() {
storage.mode(e) <- "numeric"
pm <- period.prod(x, e)
}

# test conversion from intraday to daily or lower frequency
test.intraday_to_daily <- function() {
set.seed(21)
i <- as.POSIXct("2013-02-05 01:01", tz = "America/Chicago")
x <- xts(rnorm(10000), i - 10000:1 * 60)
d <- to.daily(x)
dateseq <- seq(as.Date("2013-01-29"), as.Date("2013-02-05"), "day")
checkEqualsNumeric(index(d), dateseq)
}

0 comments on commit bfcbe50

Please sign in to comment.