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

Concatenate TSFrames.TSFrame #199

Open
femtotrader opened this issue Apr 25, 2024 · 3 comments
Open

Concatenate TSFrames.TSFrame #199

femtotrader opened this issue Apr 25, 2024 · 3 comments

Comments

@femtotrader
Copy link

Hello,

I have 3 TSFrames.TSFrame defined like so

julia> using Dates

julia> using TSFrames

julia> const dates = Date(2020, 12, 31):Month(1):Date(2021, 12, 31)
Date("2020-12-31"):Month(1):Date("2021-12-31")

julia> const TSLA = [
           235.22,
               264.51,
           225.16,
           222.64,
           236.48,
           208.40,
           226.56,
           229.06,
           245.24,
           258.49,
               371.33,
           381.58,
           352.26,
       ]
13-element Vector{Float64}:
 235.22
 264.51
 225.16
 222.64
 236.48
 208.4
 226.56
 229.06
 245.24
 258.49
 371.33
 381.58
 352.26

julia> const NFLX = [
           540.73,
               532.39,
           538.85,
           521.66,
           513.47,
           502.81,
           528.21,
           517.57,
           569.19,
           610.34,
           690.31,
           641.90,
           602.44,
       ]
13-element Vector{Float64}:
 540.73
 532.39
 538.85
 521.66
 513.47
 502.81
 528.21
 517.57
 569.19
 610.34
 690.31
 641.9
 602.44

julia> const MSFT = [
           222.42,
           231.96,
           232.38,
           235.77,
           252.18,
           249.68,
           270.90,
           284.91,
           301.88,
           281.92,
           331.62,
           330.59,
           336.32,
       ]
13-element Vector{Float64}:
 222.42
 231.96
 232.38
 235.77
 252.18
 249.68
 270.9
 284.91
 301.88
 281.92
 331.62
 330.59
 336.32

julia> tsla = TSFrame(TSLA, dates, colnames=[:TSLA])
13×1 TSFrame with Date Index
 Index       TSLA
 Date        Float64
─────────────────────
 2020-12-31   235.22
 2021-01-31   264.51
 2021-02-28   225.16
 2021-03-31   222.64
 2021-04-30   236.48
 2021-05-31   208.4
 2021-06-30   226.56
 2021-07-31   229.06
 2021-08-31   245.24
 2021-09-30   258.49
 2021-10-31   371.33
 2021-11-30   381.58
 2021-12-31   352.26

julia> nflx = TSFrame(NFLX, dates, colnames=[:NFLX])
13×1 TSFrame with Date Index
 Index       NFLX
 Date        Float64
─────────────────────
 2020-12-31   540.73
 2021-01-31   532.39
 2021-02-28   538.85
 2021-03-31   521.66
 2021-04-30   513.47
 2021-05-31   502.81
 2021-06-30   528.21
 2021-07-31   517.57
 2021-08-31   569.19
 2021-09-30   610.34
 2021-10-31   690.31
 2021-11-30   641.9
 2021-12-31   602.44

julia> msft = TSFrame(MSFT, dates, colnames=[:MSFT])
13×1 TSFrame with Date Index
 Index       MSFT
 Date        Float64
─────────────────────
 2020-12-31   222.42
 2021-01-31   231.96
 2021-02-28   232.38
 2021-03-31   235.77
 2021-04-30   252.18
 2021-05-31   249.68
 2021-06-30   270.9
 2021-07-31   284.91
 2021-08-31   301.88
 2021-09-30   281.92
 2021-10-31   331.62
 2021-11-30   330.59
 2021-12-31   336.32

I don't understand why concatenation doesn't work (at least doesn't work as I'm expecting)

julia> vcat(tsla, nflx, msft)
3-element Vector{TSFrame}:
 13×1 TSFrame with Date Index
 Index       TSLA
 Date        Float64
─────────────────────
 2020-12-31   235.22
 2021-01-31   264.51
 2021-02-28   225.16
 2021-03-31   222.64
 2021-04-30   236.48
 2021-05-31   208.4
 2021-06-30   226.56
 2021-07-31   229.06
 2021-08-31   245.24
 2021-09-30   258.49
 2021-10-31   371.33
 2021-11-30   381.58
 2021-12-31   352.26
 13×1 TSFrame with Date Index
 Index       NFLX
 Date        Float64
─────────────────────
 2020-12-31   540.73
 2021-01-31   532.39
 2021-02-28   538.85
 2021-03-31   521.66
 2021-04-30   513.47
 2021-05-31   502.81
 2021-06-30   528.21
 2021-07-31   517.57
 2021-08-31   569.19
 2021-09-30   610.34
 2021-10-31   690.31
 2021-11-30   641.9
 2021-12-31   602.44
 13×1 TSFrame with Date Index
 Index       MSFT
 Date        Float64
─────────────────────
 2020-12-31   222.42
 2021-01-31   231.96
 2021-02-28   232.38
 2021-03-31   235.77
 2021-04-30   252.18
 2021-05-31   249.68
 2021-06-30   270.9
 2021-07-31   284.91
 2021-08-31   301.88
 2021-09-30   281.92
 2021-10-31   331.62
 2021-11-30   330.59
 2021-12-31   336.32

julia> vcat(tsla, nflx)
26×2 TSFrame with Date Index
 Index       TSLA        NFLX
 Date        Float64?    Float64?
────────────────────────────────────
 2020-12-31      235.22  missing
 2020-12-31  missing         540.73
 2021-01-31      264.51  missing
 2021-01-31  missing         532.39
 2021-02-28      225.16  missing
 2021-02-28  missing         538.85
 2021-03-31      222.64  missing
 2021-03-31  missing         521.66
 2021-04-30      236.48  missing
 2021-04-30  missing         513.47
 2021-05-31      208.4   missing
 2021-05-31  missing         502.81
 2021-06-30      226.56  missing
 2021-06-30  missing         528.21
 2021-07-31      229.06  missing
 2021-07-31  missing         517.57
 2021-08-31      245.24  missing
 2021-08-31  missing         569.19
 2021-09-30      258.49  missing
 2021-09-30  missing         610.34
 2021-10-31      371.33  missing
 2021-10-31  missing         690.31
 2021-11-30      381.58  missing
 2021-11-30  missing         641.9
 2021-12-31      352.26  missing
 2021-12-31  missing         602.44

I was expecting to get something like

julia> TSFrame([TSLA NFLX MSFT], dates, colnames = [:TSLA, :NFLX, :MSFT])
13×3 TSFrame with Date Index
 Index       TSLA     NFLX     MSFT
 Date        Float64  Float64  Float64
───────────────────────────────────────
 2020-12-31   235.22   540.73   222.42
 2021-01-31   264.51   532.39   231.96
 2021-02-28   225.16   538.85   232.38
 2021-03-31   222.64   521.66   235.77
 2021-04-30   236.48   513.47   252.18
 2021-05-31   208.4    502.81   249.68
 2021-06-30   226.56   528.21   270.9
 2021-07-31   229.06   517.57   284.91
 2021-08-31   245.24   569.19   301.88
 2021-09-30   258.49   610.34   281.92
 2021-10-31   371.33   690.31   331.62
 2021-11-30   381.58   641.9    330.59
 2021-12-31   352.26   602.44   336.32

Any idea?

@san-ath
Copy link
Contributor

san-ath commented Apr 25, 2024

Hi @femtotrader, using "join" instead of "vcat" will give you the result you're looking for.

julia> join(tsla, nflx, msft)

13×3 TSFrame with Date Index
 Index       TSLA      NFLX      MSFT     
 Date        Float64?  Float64?  Float64? 
──────────────────────────────────────────
 2020-12-31    235.22    540.73    222.42
 2021-01-31    264.51    532.39    231.96
 2021-02-28    225.16    538.85    232.38
 2021-03-31    222.64    521.66    235.77
 2021-04-30    236.48    513.47    252.18
 2021-05-31    208.4     502.81    249.68
 2021-06-30    226.56    528.21    270.9
 2021-07-31    229.06    517.57    284.91
 2021-08-31    245.24    569.19    301.88
 2021-09-30    258.49    610.34    281.92
 2021-10-31    371.33    690.31    331.62
 2021-11-30    381.58    641.9     330.59
 2021-12-31    352.26    602.44    336.32

@femtotrader
Copy link
Author

Thanks @san-ath but I don't understand why type is now Float64? and not just Float64 (there is not missing here)

@chiraganand
Copy link
Member

Thanks @san-ath but I don't understand why type is now Float64? and not just Float64 (there is not missing here)

It is assumed that there could be missings in the arguments and the function needs to handle them. This is the DataFrames default behaviour but it does let the user suggest what to do with missings (matchmissing parameter). Perhaps, this can be introduced in TSFrames too.

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

3 participants