Indexing
Overview
One of the chief aims of the Temporal.jl
package is to simplify the process of extracting a desired subset from a time series dataset. To that end, there are quite a few different methods by which one can index specific rows/columns of a TS
object.
One goal has been to keep as much of the relevant indexing operations from the base Array
type as possible to maintain consistency. However, there are certain indexing idioms that are specifically more familiar and meaningful to tabular time series data, particularly when prototyping in the REPL.
In other words, if you want to use standard Array
indexing syntax, it should work as you would expect, but you should also be able to essentially say, "give me all the observations from the year 2017 in the price column."
Numerical Indexing
Integer
julia> using Temporal
julia> X = TS(cumsum(randn(252, 4), dims=1)) + 100.0
252x4 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A B C D
2019-05-16 98.6866 99.0654 100.4319 99.7904
2019-05-17 100.0482 100.015 99.8082 100.1738
2019-05-18 100.387 100.5032 99.2186 100.4274
2019-05-19 99.611 99.5853 99.4851 101.0283
2019-05-20 101.4635 99.2436 99.7689 101.4699
2019-05-21 100.6716 97.4302 99.1906 101.6606
2019-05-22 101.582 96.4242 98.4857 100.5608
⋮
2020-01-15 102.7939 60.6802 94.471 75.155
2020-01-16 102.0165 62.054 93.9059 74.8833
2020-01-17 103.0108 61.0891 92.9438 75.3359
2020-01-18 103.3658 61.57 92.1186 73.3703
2020-01-19 103.8781 62.028 92.3801 73.5632
2020-01-20 103.456 62.0668 91.7383 74.541
2020-01-21 103.1441 60.5328 90.5061 76.0917
2020-01-22 102.7623 60.5146 90.4418 76.7895
julia> X[1]
1x4 TS{Float64,Dates.Date}: 2019-05-16 to 2019-05-16
Index A B C D
2019-05-16 98.6866 99.0654 100.4319 99.7904
julia> X[1, :]
1x4 TS{Float64,Dates.Date}: 2019-05-16 to 2019-05-16
Index A B C D
2019-05-16 98.6866 99.0654 100.4319 99.7904
julia> X[:, 1]
252x1 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A
2019-05-16 98.6866
2019-05-17 100.0482
2019-05-18 100.387
2019-05-19 99.611
2019-05-20 101.4635
2019-05-21 100.6716
2019-05-22 101.582
⋮
2020-01-15 102.7939
2020-01-16 102.0165
2020-01-17 103.0108
2020-01-18 103.3658
2020-01-19 103.8781
2020-01-20 103.456
2020-01-21 103.1441
2020-01-22 102.7623
julia> X[1, 1]
1x1 TS{Float64,Dates.Date}: 2019-05-16 to 2019-05-16
Index A
2019-05-16 98.6866
Boolean
julia> using Temporal
julia> X = TS(cumsum(randn(252, 4), dims=1)) + 100.0
252x4 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A B C D
2019-05-16 100.3316 99.5983 101.007 100.3741
2019-05-17 101.3038 99.0726 100.9251 100.9912
2019-05-18 100.4854 101.1175 100.7057 101.157
2019-05-19 99.7628 101.4995 99.2659 100.6376
2019-05-20 98.7862 102.7096 98.8789 101.9109
2019-05-21 98.2048 101.3716 99.249 99.1243
2019-05-22 98.6648 103.193 98.4795 97.6652
⋮
2020-01-15 84.6467 128.0249 92.5995 109.8005
2020-01-16 85.49 128.8548 94.2217 110.8611
2020-01-17 84.1138 128.2569 92.8791 111.3226
2020-01-18 84.2796 128.7231 95.3016 111.5887
2020-01-19 82.7441 129.8331 94.7279 111.4788
2020-01-20 84.7533 129.0773 93.1745 109.0643
2020-01-21 86.0232 127.9056 93.4819 107.2712
2020-01-22 87.3607 129.1558 95.3203 106.6567
julia> X[trues(size(X,1)), :]
252x4 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A B C D
2019-05-16 100.3316 99.5983 101.007 100.3741
2019-05-17 101.3038 99.0726 100.9251 100.9912
2019-05-18 100.4854 101.1175 100.7057 101.157
2019-05-19 99.7628 101.4995 99.2659 100.6376
2019-05-20 98.7862 102.7096 98.8789 101.9109
2019-05-21 98.2048 101.3716 99.249 99.1243
2019-05-22 98.6648 103.193 98.4795 97.6652
⋮
2020-01-15 84.6467 128.0249 92.5995 109.8005
2020-01-16 85.49 128.8548 94.2217 110.8611
2020-01-17 84.1138 128.2569 92.8791 111.3226
2020-01-18 84.2796 128.7231 95.3016 111.5887
2020-01-19 82.7441 129.8331 94.7279 111.4788
2020-01-20 84.7533 129.0773 93.1745 109.0643
2020-01-21 86.0232 127.9056 93.4819 107.2712
2020-01-22 87.3607 129.1558 95.3203 106.6567
julia> X[rand(Bool, size(X,1)), 1]
142x1 TS{Float64,Dates.Date}: 2019-05-17 to 2020-01-22
Index A
2019-05-17 101.3038
2019-05-19 99.7628
2019-05-20 98.7862
2019-05-21 98.2048
2019-05-22 98.6648
2019-05-25 93.3089
2019-05-26 93.4905
⋮
2020-01-11 84.6203
2020-01-12 84.4411
2020-01-14 83.7127
2020-01-16 85.49
2020-01-17 84.1138
2020-01-20 84.7533
2020-01-21 86.0232
2020-01-22 87.3607
julia> X[rand(Bool, size(X,1)), [true, false, false, false]]
122x1 TS{Float64,Dates.Date}: 2019-05-18 to 2020-01-21
Index A
2019-05-18 100.4854
2019-05-19 99.7628
2019-05-20 98.7862
2019-05-21 98.2048
2019-05-24 95.2493
2019-05-25 93.3089
2019-05-26 93.4905
⋮
2019-12-30 81.0322
2020-01-01 82.2274
2020-01-02 84.0478
2020-01-04 85.7576
2020-01-06 85.8758
2020-01-08 86.2656
2020-01-13 84.5028
2020-01-21 86.0232
Arrays & Ranges
julia> using Temporal
julia> X = TS(cumsum(randn(252, 4), dims=1)) + 100.0
252x4 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A B C D
2019-05-16 100.3975 99.1322 100.5429 99.7729
2019-05-17 100.3742 100.6395 102.4592 101.26
2019-05-18 99.7855 101.7928 101.2358 100.4166
2019-05-19 101.1186 103.6874 102.5896 103.2072
2019-05-20 101.5145 104.0759 102.7336 101.8904
2019-05-21 101.5123 103.4743 101.9031 100.2464
2019-05-22 100.207 103.834 101.8898 99.2743
⋮
2020-01-15 107.2303 110.0305 102.8981 108.1753
2020-01-16 105.9696 110.6999 102.2371 107.8664
2020-01-17 105.5931 112.0221 101.0748 107.1259
2020-01-18 106.8437 112.8432 99.7459 106.5594
2020-01-19 108.3786 110.1217 97.1794 108.6165
2020-01-20 108.1244 110.8715 95.989 108.566
2020-01-21 107.8129 111.494 94.3686 109.5921
2020-01-22 107.9921 111.906 94.6396 109.6239
julia> X[1:10, :]
10x4 TS{Float64,Dates.Date}: 2019-05-16 to 2019-05-25
Index A B C D
2019-05-16 100.3975 99.1322 100.5429 99.7729
2019-05-17 100.3742 100.6395 102.4592 101.26
2019-05-18 99.7855 101.7928 101.2358 100.4166
2019-05-19 101.1186 103.6874 102.5896 103.2072
2019-05-20 101.5145 104.0759 102.7336 101.8904
2019-05-21 101.5123 103.4743 101.9031 100.2464
2019-05-22 100.207 103.834 101.8898 99.2743
2019-05-23 101.4168 103.5702 102.1225 98.9957
2019-05-24 101.6283 103.9381 102.2202 98.6222
2019-05-25 99.1156 104.9004 102.3365 98.6609
julia> X[end-100:end, 2:3]
101x2 TS{Float64,Dates.Date}: 2019-10-14 to 2020-01-22
Index B C
2019-10-14 87.3893 105.9999
2019-10-15 87.9511 106.6744
2019-10-16 89.4174 104.6139
2019-10-17 90.2724 104.2003
2019-10-18 89.4847 103.947
2019-10-19 89.6332 104.3067
2019-10-20 88.4649 103.8917
⋮
2020-01-15 110.0305 102.8981
2020-01-16 110.6999 102.2371
2020-01-17 112.0221 101.0748
2020-01-18 112.8432 99.7459
2020-01-19 110.1217 97.1794
2020-01-20 110.8715 95.989
2020-01-21 111.494 94.3686
2020-01-22 111.906 94.6396
julia> X[end, 2:end]
1x3 TS{Float64,Dates.Date}: 2020-01-22 to 2020-01-22
Index B C D
2020-01-22 111.906 94.6396 109.6239
Symbol Indexing
You can also index specific columns you want using the fields
member of the TS
object, so that columns can be fetched by name rather than by numerical index.
julia> using Temporal
julia> X = TS(cumsum(randn(252, 4), dims=1)) + 100.0
252x4 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A B C D
2019-05-16 99.3698 99.8706 102.0989 101.8872
2019-05-17 98.2247 99.9726 103.245 102.2658
2019-05-18 98.2041 97.5618 103.5207 103.4663
2019-05-19 97.1498 97.6927 102.039 102.3704
2019-05-20 95.506 99.0587 100.1599 102.4947
2019-05-21 95.1027 97.3341 100.723 102.6998
2019-05-22 95.1512 95.9339 99.7611 101.5261
⋮
2020-01-15 116.2069 94.0465 94.2906 110.6419
2020-01-16 116.7548 94.0204 93.1235 111.4485
2020-01-17 115.7681 96.0353 90.3166 113.614
2020-01-18 114.2811 96.1602 90.881 112.7268
2020-01-19 115.7526 97.6689 91.3106 112.0471
2020-01-20 115.0424 96.2344 91.1686 112.5294
2020-01-21 113.1797 95.0827 91.8432 113.5048
2020-01-22 113.3294 96.3003 92.7214 115.1052
julia> X[:, :A]
252x1 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index A
2019-05-16 99.3698
2019-05-17 98.2247
2019-05-18 98.2041
2019-05-19 97.1498
2019-05-20 95.506
2019-05-21 95.1027
2019-05-22 95.1512
⋮
2020-01-15 116.2069
2020-01-16 116.7548
2020-01-17 115.7681
2020-01-18 114.2811
2020-01-19 115.7526
2020-01-20 115.0424
2020-01-21 113.1797
2020-01-22 113.3294
julia> X[:, [:B, :D]]
252x2 TS{Float64,Dates.Date}: 2019-05-16 to 2020-01-22
Index B D
2019-05-16 99.8706 101.8872
2019-05-17 99.9726 102.2658
2019-05-18 97.5618 103.4663
2019-05-19 97.6927 102.3704
2019-05-20 99.0587 102.4947
2019-05-21 97.3341 102.6998
2019-05-22 95.9339 101.5261
⋮
2020-01-15 94.0465 110.6419
2020-01-16 94.0204 111.4485
2020-01-17 96.0353 113.614
2020-01-18 96.1602 112.7268
2020-01-19 97.6689 112.0471
2020-01-20 96.2344 112.5294
2020-01-21 95.0827 113.5048
2020-01-22 96.3003 115.1052
String Indexing
One of the more powerful features of Temporal's indexing functionality is that you can index rows of a TS
object using String
s formatted in such a way as to express specific periods of time in a natural idiomatic way. (If you have used the xts
package in R this functionality will feel very familiar.)
julia> using Dates, Temporal
julia> t = Date(2016,1,1):Day(1):Date(2017,12,31)
2016-01-01:1 day:2017-12-31
julia> X = TS(cumsum(randn(length(t), 4), dims=1), t) + 100.0
731x4 TS{Float64,Dates.Date}: 2016-01-01 to 2017-12-31
Index A B C D
2016-01-01 100.8633 99.7952 101.3307 99.0324
2016-01-02 101.8123 98.1013 100.2852 99.3293
2016-01-03 101.7068 99.9488 100.4506 98.694
2016-01-04 101.9645 99.4779 100.3221 99.6773
2016-01-05 101.7437 97.4134 100.9236 99.7543
2016-01-06 101.7007 100.457 103.4613 99.4413
2016-01-07 102.0172 98.7197 103.9259 99.2054
⋮
2017-12-24 153.5977 72.3186 78.8151 68.6672
2017-12-25 153.0583 72.3795 78.8977 67.9289
2017-12-26 152.1142 72.737 78.2958 66.4599
2017-12-27 153.0937 72.4505 78.005 65.6427
2017-12-28 153.6748 71.7049 78.6277 64.9155
2017-12-29 153.4155 72.6787 78.3466 64.6822
2017-12-30 154.1587 73.506 79.2005 64.4994
2017-12-31 153.9155 73.7854 78.7804 64.4116
julia> X["2017-07-01"] # single day
1x4 TS{Float64,Dates.Date}: 2017-07-01 to 2017-07-01
Index A B C D
2017-07-01 132.3045 95.0054 92.1519 86.4811
julia> X["2016"] # whole year
366x4 TS{Float64,Dates.Date}: 2016-01-01 to 2016-12-31
Index A B C D
2016-01-01 100.8633 99.7952 101.3307 99.0324
2016-01-02 101.8123 98.1013 100.2852 99.3293
2016-01-03 101.7068 99.9488 100.4506 98.694
2016-01-04 101.9645 99.4779 100.3221 99.6773
2016-01-05 101.7437 97.4134 100.9236 99.7543
2016-01-06 101.7007 100.457 103.4613 99.4413
2016-01-07 102.0172 98.7197 103.9259 99.2054
⋮
2016-12-24 103.994 110.8149 77.7655 87.2072
2016-12-25 105.6431 110.3697 78.9606 87.9523
2016-12-26 104.3643 110.4883 80.2921 88.0832
2016-12-27 103.5714 110.0237 79.7147 89.4325
2016-12-28 103.0032 111.2518 78.7142 89.2586
2016-12-29 105.5631 112.1471 79.3723 88.9228
2016-12-30 104.2963 111.6082 79.5318 89.7294
2016-12-31 103.67 110.2627 79.615 91.1965
julia> X["2016-09-15/"] # everything after a specific day
473x4 TS{Float64,Dates.Date}: 2016-09-15 to 2017-12-31
Index A B C D
2016-09-15 116.5816 100.8731 79.8184 96.6885
2016-09-16 114.6219 101.4774 78.5326 96.7551
2016-09-17 115.6208 100.4858 77.8231 95.4912
2016-09-18 117.1853 99.7012 78.2792 96.0066
2016-09-19 118.1032 100.0554 76.9568 96.7463
2016-09-20 115.077 99.852 74.5211 96.2612
2016-09-21 114.4666 100.3206 75.4855 96.3743
⋮
2017-12-24 153.5977 72.3186 78.8151 68.6672
2017-12-25 153.0583 72.3795 78.8977 67.9289
2017-12-26 152.1142 72.737 78.2958 66.4599
2017-12-27 153.0937 72.4505 78.005 65.6427
2017-12-28 153.6748 71.7049 78.6277 64.9155
2017-12-29 153.4155 72.6787 78.3466 64.6822
2017-12-30 154.1587 73.506 79.2005 64.4994
2017-12-31 153.9155 73.7854 78.7804 64.4116
julia> X["/2017-07-01"] # everything up through a specific month
548x4 TS{Float64,Dates.Date}: 2016-01-01 to 2017-07-01
Index A B C D
2016-01-01 100.8633 99.7952 101.3307 99.0324
2016-01-02 101.8123 98.1013 100.2852 99.3293
2016-01-03 101.7068 99.9488 100.4506 98.694
2016-01-04 101.9645 99.4779 100.3221 99.6773
2016-01-05 101.7437 97.4134 100.9236 99.7543
2016-01-06 101.7007 100.457 103.4613 99.4413
2016-01-07 102.0172 98.7197 103.9259 99.2054
⋮
2017-06-24 130.0783 103.1732 96.7954 83.4299
2017-06-25 131.432 101.1058 95.58 84.5946
2017-06-26 131.6053 102.3979 94.9342 85.0463
2017-06-27 131.6623 100.0243 96.4609 85.8571
2017-06-28 131.9796 100.3973 96.0927 86.4833
2017-06-29 130.8949 98.5101 95.4329 88.1328
2017-06-30 130.4756 97.1464 93.4607 87.2979
2017-07-01 132.3045 95.0054 92.1519 86.4811
julia> X["2016-09-15/2017-07-01"] # mix & match
290x4 TS{Float64,Dates.Date}: 2016-09-15 to 2017-07-01
Index A B C D
2016-09-15 116.5816 100.8731 79.8184 96.6885
2016-09-16 114.6219 101.4774 78.5326 96.7551
2016-09-17 115.6208 100.4858 77.8231 95.4912
2016-09-18 117.1853 99.7012 78.2792 96.0066
2016-09-19 118.1032 100.0554 76.9568 96.7463
2016-09-20 115.077 99.852 74.5211 96.2612
2016-09-21 114.4666 100.3206 75.4855 96.3743
⋮
2017-06-24 130.0783 103.1732 96.7954 83.4299
2017-06-25 131.432 101.1058 95.58 84.5946
2017-06-26 131.6053 102.3979 94.9342 85.0463
2017-06-27 131.6623 100.0243 96.4609 85.8571
2017-06-28 131.9796 100.3973 96.0927 86.4833
2017-06-29 130.8949 98.5101 95.4329 88.1328
2017-06-30 130.4756 97.1464 93.4607 87.2979
2017-07-01 132.3045 95.0054 92.1519 86.4811