Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ops.xts on dim-less xts and matrix creates malformed xts objects #295

Closed
joshuaulrich opened this issue Apr 28, 2019 · 0 comments
Closed
Assignees
Labels
Milestone

Comments

@joshuaulrich
Copy link
Owner

Several permutations of calling binary operators on xts and matrix arguments results in a malformed xts object. If the xts object does not have a dim attribute, the result has a c("xts", "zoo") class, but no index attribute. The xts result will also have rownames if the matrix does.

data(sample_matrix)
x <- as.xts(sample_matrix[1:5, c(1,4)])
d <- coredata(x)
m <- as.matrix(x)
xv <- drop(x[,1])

# all these are okay: index, class, dim, dimnames (no rownames)
attributes(x-d)
attributes(x-m)
attributes(x-d[,1])
attributes(x-m[,1])

# these are various degrees of wrong
attributes(xv-d)      # no index
attributes(xv-m)      # no index, has rownames
attributes(xv-d[,1])  # okay
attributes(xv-m[,1])  # has names

# not clear what is the correct behavior here
# these pick up the dim attribute from the 1-column matrix
attributes(xv-d[, 1, drop = FALSE])
attributes(xv-m[, 1, drop = FALSE])  # has rownames

Session Info

These were found as part of testing #245.

For reference, the behavior of the last 2 results is consistent with the current version (0.11-2):

data(sample_matrix)
x <- as.xts(sample_matrix[1:5, c(1,4)])
d <- coredata(x)
m <- as.matrix(x)
xv <- drop(x[,1])

attributes(xv-d[, 1, drop = FALSE])
# $index
# [1] 1167717600 1167804000 1167890400 1167976800 1168063200
# attr(,"tzone")
# [1] ""
# attr(,"tclass")
# [1] "POSIXct" "POSIXt" 
# 
# $class
# [1] "xts" "zoo"
# 
# $.indexCLASS
# [1] "POSIXct" "POSIXt" 
# 
# $tclass
# [1] "POSIXct" "POSIXt" 
# 
# $.indexTZ
# [1] ""
# 
# $tzone
# [1] ""
# 
# $dim
# [1] 5 1
# 
# $dimnames
# $dimnames[[1]]
# NULL
# 
# $dimnames[[2]]
# [1] "Open"

attributes(xv-m[, 1, drop = FALSE])  # has rownames
# $index
# [1] 1167717600 1167804000 1167890400 1167976800 1168063200
# attr(,"tzone")
# [1] ""
# attr(,"tclass")
# [1] "POSIXct" "POSIXt" 
# 
# $class
# [1] "xts" "zoo"
# 
# $.indexCLASS
# [1] "POSIXct" "POSIXt" 
# 
# $tclass
# [1] "POSIXct" "POSIXt" 
# 
# $.indexTZ
# [1] ""
# 
# $tzone
# [1] ""
# 
# $dim
# [1] 5 1
# 
# $dimnames
# $dimnames[[1]]
# [1] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-06"
# 
# $dimnames[[2]]
# [1] "Open"
@joshuaulrich joshuaulrich self-assigned this Apr 28, 2019
joshuaulrich added a commit that referenced this issue May 6, 2019
The result of Ops.xts() on an xts and a matrix will have rownames if
the matrix has rownames (e.g. if the matrix was created via as.xts()).

Always remove rownames from the Ops.xts() result, and remove dimnames
entirely if the result does not have colnames.

Add tests that exercise subtraction. Need to add tests that exercise
the relational operators.

See #295.
joshuaulrich added a commit that referenced this issue May 6, 2019
Calling Ops.xts() on a matrix and a xts object without a dim attribute
can result in an xts object without an index, and/or with a names
attribute.

Ensure there is never a names attribute, and that we always check if
the index exists.

Tests only exercise the subtraction operator. There should be tests
that use relational operators.

See #295.
joshuaulrich added a commit that referenced this issue May 6, 2019
These tests check all mathematic operations on all double, integer,
and logical types of xts objects. Still need to check relational
operators.

See #295.
joshuaulrich added a commit that referenced this issue May 12, 2019
Maintain the index type even though it is zero-length.

Previous behavior always returned a numeric index when Ops.xts() was
called on two xts objects that do not share any common index elements.

See #295.
joshuaulrich added a commit that referenced this issue May 12, 2019
The Ops unit tests only covered arithmetic operations. Add tests for
relational operations. We have to explicitly convert `E` to logical
because the `E[] <-` calls do not change the storage type.

Also rename the tests to use a more general 'ops' prefix.

test.ops_xts_no_dim_vector() failed for `&` on numeric types because
Ops.xts() would add dims. Update Ops.xts() to remove dims if neither
argument has a dim attribute.

See #295.
@joshuaulrich joshuaulrich added this to the 0.12-0 milestone Jul 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant