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

xts character 2nd subsetting a zero-width xts returns integer(0) instead of Date of length 0 #255

Closed
AndreMikulec opened this issue Jul 22, 2018 · 5 comments
Labels
feature request New features
Milestone

Comments

@AndreMikulec
Copy link

AndreMikulec commented Jul 22, 2018

Hello,

Given a a zero-width xts, xts character 2nd subsetting returns integer(0) instead of Date of length 0. For example:

library(xts)
testxts <- xts(1,as.Date('2018-07-18'))

# no dates in 2020

> testxts['2020']
     [,1]

# is zero width

> str(testxts['2020'])
An 'xts' object of zero-width

# is Date of length 0
# (no dates in 2020)

> index(testxts['2020'])
Date of length 0

# no dates in 2020 and 2021

> testxts['2020']['2021']
     [,1]

> str(testxts['2020']['2021'])
An 'xts' object of zero-width

# but index(.) should be 'Date of length 0'

> index(testxts['2020']['2021'])
integer(0)

Maybe a solution is this
In xts/R/index.R

fixInNamespace(x = "index.xts", ns = "xts")

replace
    if (length(x.index) == 0)
        return(integer())
by
    if (length(x.index) == 0)
        # return(integer())
        return(x.index)

SAVE notepad
> index(testxts['2020'])
Date of length 0

# So this works but the index class is wrong

> index(testxts['2020']['2021'])
POSIXct of length 0
fixInNamespace(x = "[.xts", ns = "xts")

replace
        x.tmp <- .xts(rep(NA,length(i)), .index(x)[i], dimnames=list(NULL, colnames(x)))

by ( replace .xts using xts and replace .index using index )
        # x.tmp <- .xts(rep(NA,length(i)), .index(x)[i], dimnames=list(NULL, colnames(x)))
          x.tmp <-  xts(rep(NA,length(i)),  index(x)[i], dimnames=list(NULL, colnames(x)))

SAVE notepad

So now this works

> index(testxts['2020'])
Date of length 0

> index(testxts['2020']['2021'])
Date of length 0

So now all of these cases work

> str(testxts['2018-07-18'])
An 'xts' object on 2018-07-18/2018-07-18 containing:
  Data: num [1, 1] 1
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:
 NULL

> index(testxts['2018-07-18'])
[1] "2018-07-18"

> class(index(testxts['2018-07-18']))
[1] "Date"
> str(testxts[zoo::as.Date('2018-07-30')])
An 'xts' object of zero-width

> index(testxts[zoo::as.Date('2018-07-30')])
Date of length 0

> class(index(testxts[zoo::as.Date('2018-07-30')]))
[1] "Date"
> str(testxts[zoo::as.Date('2018-07-30')][zoo::as.Date('2018-07-18')])
An 'xts' object of zero-width

> index(testxts[zoo::as.Date('2018-07-30')][zoo::as.Date('2018-07-18')])
Date of length 0

> class(index(testxts[zoo::as.Date('2018-07-30')][zoo::as.Date('2018-07-18')]))
[1] "Date"
> str(testxts[zoo::as.Date('2018-07-18')][zoo::as.Date('2018-07-30')])
An 'xts' object of zero-width

> index(testxts[zoo::as.Date('2018-07-30')][zoo::as.Date('2018-07-18')])
Date of length 0

> class(index(testxts[zoo::as.Date('2018-07-30')][zoo::as.Date('2018-07-18')]))
[1] "Date"
> str(testxts[c(zoo::as.Date('2018-07-30'), zoo::as.Date('2018-07-18'))])
An 'xts' object on 2018-07-18/2018-07-18 containing:
  Data: num [1, 1] 1
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:
 NULL

> index(testxts[c(zoo::as.Date('2018-07-30'), zoo::as.Date('2018-07-18'))])
[1] "2018-07-18"

> class(index(testxts[c(zoo::as.Date('2018-07-30'), zoo::as.Date('2018-07-18'))]))
[1] "Date"
>
> str(testxts['2018-07-30'])
An 'xts' object of zero-width

> index(testxts['2018-07-30'])
Date of length 0

> class(index(testxts['2018-07-30']))
[1] "Date"
> str(testxts['2018-07-18']['2018-07-30'])
An 'xts' object of zero-width

> index(testxts['2018-07-18']['2018-07-30'])
Date of length 0

> class(index(testxts['2018-07-18']['2018-07-30']))
[1] "Date"
> devtools::session_info()
Session info ------------------------------------------------------------------
 setting  value
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, mingw32
 ui       RTerm
 language (EN)
 collate  English_United States.1252
 tz       America/Chicago
 date     2018-07-22

Packages ----------------------------------------------------------------------
 package    * version date       source
 base       * 3.5.1   2018-07-02 local
 compiler     3.5.1   2018-07-02 local
 datasets   * 3.5.1   2018-07-02 local
 devtools     1.13.6  2018-06-27 CRAN (R 3.5.1)
 digest       0.6.15  2018-01-28 CRAN (R 3.5.0)
 graphics   * 3.5.1   2018-07-02 local
 grDevices  * 3.5.1   2018-07-02 local
 grid         3.5.1   2018-07-02 local
 lattice      0.20-35 2017-03-25 CRAN (R 3.5.0)
 magrittr   * 1.5     2014-11-22 CRAN (R 3.5.0)
 memoise      1.1.0   2017-04-21 CRAN (R 3.5.0)
 methods    * 3.5.1   2018-07-02 local
 rstudioapi   0.7     2017-09-07 CRAN (R 3.5.0)
 stats      * 3.5.1   2018-07-02 local
 tools        3.5.1   2018-07-02 local
 utils      * 3.5.1   2018-07-02 local
 withr        2.1.2   2018-04-27 Github (jimhester/withr@79d7b0d)
 xts        * 0.11-0  2018-07-16 CRAN (R 3.5.1)
 zoo        * 1.8-3   2018-07-16 CRAN (R 3.5.1)
>
@joshuaulrich
Copy link
Owner

Please do not open a new issue to discuss a solution proposed in another issue. That splits the discussion into multiple places, which makes it harder to keep track of the relevant details.

@AndreMikulec
Copy link
Author

This issue is a different problem than what is in
"
Can't subset empty xts by date anymore #252
#252
"

The problem presented in this issue is the following

 the index(.) result should be 'Date of length 0' and not integer(0) and not POSIXct of length 0.
> str(testxts['2020']['2021'])
An 'xts' object of zero-width

# but index(.) should be 'Date of length 0'

> index(testxts['2020']['2021'])
integer(0)

So, this issue, presents two(2) one line fixes( replace->by) (replace->by)
and issue
"
Can't subset empty xts by date anymore #252
#252
"
presents another one(1) line fix(replace->by).

@joshuaulrich
Copy link
Owner

joshuaulrich commented Jul 23, 2018

My apologies. It wasn't clear to me that this issue did not depend on the suggested solution in the other issue you referenced.

I deleted the comments about the other issue from your initial post, so others (including future me) aren't confused by them.

@joshuaulrich joshuaulrich reopened this Jul 23, 2018
@joshuaulrich
Copy link
Owner

Note that this behavior is consistent with v0.10-2:

library(xts)
packageVersion("xts")
# [1] '0.10.2'
testxts <- xts(1, as.Date("2018-07-18"))
index(testxts["2020"])
# Date of length 0
index(testxts["2020"]["2020"])
# integer(0)

@joshuaulrich
Copy link
Owner

I consider this a feature request, since the current behavior is consistent with v0.10-2. That makes this a lower priority than the bugs caused by regressions in v0.11-0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New features
Projects
None yet
Development

No branches or pull requests

2 participants