Closed dppalomar closed 1 year ago
Thanks for the report! I see that you're using xts_0.11-2. Can you please try the current development version 0.12.0 and report back?
Joshua, I tried as you said
other attached packages:
[1] xts_0.12-0 zoo_1.8-6
but still the same problem...
> test2 <- convertIndex(test, "Date")
> tclass(test2)
[1] "Date"
> test2["2014-01-01"]
[,1]
2014-01-01 0.5057137
> index(test2["2014-01-01"])
[1] "2014-01-01"
> test2[index(test2["2014-01-01"])] # What???!!! Empty!!!
[,1]
Thanks for checking! I investigated and found the cause is that convertIndex()
doesn't actually change the underlying index values. convertIndex()
is a simple wrapper around xts:::`tclass<-.xts`
, which also does not change the underlying values. So the index values are equal, even though the index attributes are different.
all.equal(.index(test), .index(test2), check.attributes=FALSE)
# [1] TRUE
We've discussed internally in the past whether or not changing the tclass
and/or tzone
attribute should change the underlying index. As this behavior shows, we've historically decided that the underlying index values should not change. Issues like your seem to happen often enough that perhaps we should reconsider.
Joshua, indeed that's what I suspected. Thanks for looking into it.
IMHO, I think this choice is very misleading and prone to errors. I don't see the point of changing the type of index without actually changing the indices.
So if you ask me, I would either totally change the indices or deprecate this wrapper. But that's my opinion. Thanks!
I'm going to make this change and do reverse dependency checks to see what, if anything, breaks. I think the only time the actual underlying index values would change is when the 'tclass' changes from one with a timezone to one without a timezone.
Part of the change will be a warning any time the 'tclass' changes from one with a timezone to one without a timezone (with an option to always suppress the warning). This should help users identify bugs and hopefully cause failing reverse dependency checks if other package authors have done it.
Description
One of the great advantages of
xts
is its flexibility in indexing the observations with dates and so. However, I found a strange behaviour after I change the indexing type of thexts
which IMHO should not happen.Minimal, reproducible example
Let's start with a toy xts and some indexing which works fine:
Now, let's change the index type from POSIXct to Date and we will get the weird behaviour:
Obviously, had I created the xts originally with Date, then there would be no problem:
Therefore, the problem comes from the change of type of index, but I don't think this should happen...
Session Info