joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
220 stars 71 forks source link

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

Closed ggrothendieck closed 2 years ago

ggrothendieck commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Perhaps split.xts could have a tz= argument to specify this.

On Wed, Oct 6, 2021 at 8:35 AM Joshua Ulrich @.***> wrote:

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

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/joshuaulrich/xts/issues/357#issuecomment-936152646, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB32F7V35AYHZJMPDZT36KTUFQ7B3ANCNFSM5FOMRRNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com

joshuaulrich commented 2 years ago

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 commented 2 years ago

Seems goo

On Fri, Oct 8, 2021 at 9:01 AM Joshua Ulrich @.***> wrote:

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"?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/joshuaulrich/xts/issues/357#issuecomment-938624327, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB32F7SHKJCIGLLGQIRJ7MTUF3TSLANCNFSM5FOMRRNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com

joshuaulrich commented 2 years ago

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.