Creating rows as per duration using datetime JULIA

Viewed 99

I am facing an issue to write a code using datetime. I had created a scenario I am working on. Can someone help me out on the code in JULIA.

Input:

Name, Channel, Duration, Start_time
John, A, 2, 16:00:00
Joseph, B, 3, 15:05:00

Output:

Name, Channel, Duration, Start_time
John, A, 2, 16:00:00
John, A, 2, 16:01:00
Joseph, B, 3, 15:05:00
Joseph, B, 3, 15:06:00
Joseph, B, 3, 15:07:00

Thank you in advance.

enter image description here

Written the same code in Python. Refer to below Link Creating rows as per duration using datetime pandas

1 Answers

Here is a solution:

julia> df = DataFrame(Name=["John", "Joseph"], Channel=["A", "B"], Duration=[2,3], Start_Time=Time.(["16:00:00", "15:05:00"]))
2×4 DataFrame
 Row │ Name    Channel  Duration  Start_Time 
     │ String  String   Int64     Time       
─────┼───────────────────────────────────────
   1 │ John    A               2  16:00:00
   2 │ Joseph  B               3  15:05:00

julia> flatten(transform(df, [:Start_Time, :Duration] => ByRow((x,y) -> x .+ Minute.(0:y-1)) => :Start_Time), :Start_Time)
5×4 DataFrame
 Row │ Name    Channel  Duration  Start_Time 
     │ String  String   Int64     Time       
─────┼───────────────────────────────────────
   1 │ John    A               2  16:00:00
   2 │ John    A               2  16:01:00
   3 │ Joseph  B               3  15:05:00
   4 │ Joseph  B               3  15:06:00
   5 │ Joseph  B               3  15:07:00

If you can assume your Name/Channel/Duration combinations are unique and you want to make sure this is the case in the code you can alternatively do:

julia> combine(groupby(df, Not(:Start_Time)), [:Start_Time, :Duration] => ((x, y) -> only(x) .+ Minute.(0:only(y)-1)) => :Start_Time)
5×4 DataFrame
 Row │ Name    Channel  Duration  Start_Time 
     │ String  String   Int64     Time       
─────┼───────────────────────────────────────
   1 │ John    A               2  16:00:00
   2 │ John    A               2  16:01:00
   3 │ Joseph  B               3  15:05:00
   4 │ Joseph  B               3  15:06:00
   5 │ Joseph  B               3  15:07:00
Related