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

split.xts() does not return named list #357

Closed
ggrothendieck opened this issue Oct 6, 2021 · 5 comments
Closed

split.xts() does not return named list #357

ggrothendieck opened this issue Oct 6, 2021 · 5 comments
Milestone

Comments

@ggrothendieck
Copy link

ggrothendieck commented Oct 6, 2021

split.xts should give a named list rather than an unnamed list in the example below. Also it gives an annoying warning message and there is no way to turn it off or control the time zone with respect to which it is split short of using split(x, as.Date(time(x))) or split(x, as.Date(time(x), tz = whatever))

x <-
structure(c(-0.108511, -0.446626999999999, 0.240643000000002, 
1.679788, -2.278705, 0.0174959999999977, -0.23011, -1.458079, 
-0.770809, -0.160931000000001, -0.884409000000002, -0.127797999999999, 
0.231928, -0.125263, -0.188977999999999, -0.343629999999997, 
-0.333669, 0.168738999999999, 1.249041, -1.41732, 0.0101289999999992, 
-0.434269, -1.328651, -0.810048999999999, -0.0380099999999999, 
0.0380099999999999, 0, 0, -0.0821299999999994, 0.0821299999999994, 
-0.0905709999999971, -0.626428000000001, 0.3538, 2.56579, -2.109532, 
0.0253410000000009, -0.243471, -1.732849, -0.921948999999998, 
-0.354142, -1.454579, 0.0786300000000004, 1.08839, -0.248750999999999, 
-0.975019000000001, -0.022001000000003, -0.474670999999997, 0.16667, 
1.165852, -4.200741, 0.0207899999999981, 0.0436310000000013, 
-1.246241, -0.48298, -0.0585299999999993, -2.37666, -0.73001, 
0.898570000000001, -0.295430000000001, -0.59243, -0.0217520000000029, 
-0.449507999999998, 0.164778999999999, 1.750981, -1.99885, 0.0310400000000008, 
-0.249700999999998, -1.548811, -0.839808999999999, -0.35397, 
-0.703519, 0.0034099999999988, -0.743641, 0, 0.602879999999999
), .Dim = c(15L, 5L), class = c("xts", "zoo"), index = structure(c(1246490100, 
1246490400, 1246490700, 1246491000, 1246491300, 1246491600, 1246491900, 
1246492200, 1246492800, 1246493100, 1246493400, 1246493700, 1246494000, 
1246494300, 1246494600), tzone = "UTC", tclass = c("POSIXct", 
"POSIXt")), .Dimnames = list(NULL, c("rrp_nsw", "rrp_qld", "rrp_sa", 
"rrp_tas", "rrp_vic")))

split(x, "days")

giving:

[[1]]
                      rrp_nsw   rrp_qld    rrp_sa   rrp_tas   rrp_vic
2009-07-01 23:15:00 -0.108511 -0.343630 -0.090571 -0.022001 -0.021752
2009-07-01 23:20:00 -0.446627 -0.333669 -0.626428 -0.474671 -0.449508
2009-07-01 23:25:00  0.240643  0.168739  0.353800  0.166670  0.164779
2009-07-01 23:30:00  1.679788  1.249041  2.565790  1.165852  1.750981
2009-07-01 23:35:00 -2.278705 -1.417320 -2.109532 -4.200741 -1.998850
2009-07-01 23:40:00  0.017496  0.010129  0.025341  0.020790  0.031040
2009-07-01 23:45:00 -0.230110 -0.434269 -0.243471  0.043631 -0.249701
2009-07-01 23:50:00 -1.458079 -1.328651 -1.732849 -1.246241 -1.548811

[[2]]
                      rrp_nsw   rrp_qld    rrp_sa  rrp_tas   rrp_vic
2009-07-02 00:00:00 -0.770809 -0.810049 -0.921949 -0.48298 -0.839809
2009-07-02 00:05:00 -0.160931 -0.038010 -0.354142 -0.05853 -0.353970
2009-07-02 00:10:00 -0.884409  0.038010 -1.454579 -2.37666 -0.703519
2009-07-02 00:15:00 -0.127798  0.000000  0.078630 -0.73001  0.003410
2009-07-02 00:20:00  0.231928  0.000000  1.088390  0.89857 -0.743641
2009-07-02 00:25:00 -0.125263 -0.082130 -0.248751 -0.29543  0.000000
2009-07-02 00:30:00 -0.188978  0.082130 -0.975019 -0.59243  0.602880

Warning messages:
1: timezone of object (UTC) is different than current timezone (). 
2: timezone of object (UTC) is different than current timezone (). 
@joshuaulrich
Copy link
Owner

joshuaulrich commented Oct 6, 2021

Thanks for the report! I agree split.xts() should be consistent with zoo and return a named list.

Do you expect the names to be 2009-07-01 and 2009-07-01?

You can disable the TZ warnings globally with options(xts_check_TZ = FALSE). That said, I'll look into why the warning appears here and see if I can quiet it.

@ggrothendieck
Copy link
Author

ggrothendieck commented Oct 6, 2021 via email

@joshuaulrich
Copy link
Owner

What do you suggest for the names? The value of the first index value, formatted to the periodicity? E.g. "days" -> Date; "months" -> yearmon; "quarters" -> yearqtr; "years" -> "%Y"?

@ggrothendieck
Copy link
Author

ggrothendieck commented Oct 9, 2021 via email

@joshuaulrich
Copy link
Owner

I looked into this a bit. The warning is thrown by print.xts(), because x has a POSIXct index with tzone = UTC, and the system TZ is not UTC.

R$ head(x)
                      rrp_nsw   rrp_qld    rrp_sa   rrp_tas   rrp_vic
2009-07-01 23:15:00 -0.108511 -0.343630 -0.090571 -0.022001 -0.021752
2009-07-01 23:20:00 -0.446627 -0.333669 -0.626428 -0.474671 -0.449508
2009-07-01 23:25:00  0.240643  0.168739  0.353800  0.166670  0.164779
2009-07-01 23:30:00  1.679788  1.249041  2.565790  1.165852  1.750981
2009-07-01 23:35:00 -2.278705 -1.417320 -2.109532 -4.200741 -1.998850
2009-07-01 23:40:00  0.017496  0.010129  0.025341  0.020790  0.031040
Warning message:
timezone of object (UTC) is different than current timezone ().

split.xts() determines when a day starts/ends based on the tzone of the xts object. In your example, it splits on UTC-days because the tzone of the object is UTC.

You need to change the xts object's tzone if you want to split on days in a different timezone. For example: tzone(x) <- "America/Chicago"; split(x, "days").

I'll name the returned list, but I don't think split.xts() should modify the xts object's timezone attribute.

@joshuaulrich joshuaulrich changed the title split.xts problems split.xts() does not return named list Feb 5, 2022
@joshuaulrich joshuaulrich added this to the 0.12.2 milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants