Search code examples
juliastatsplots

Calculate differences between different times series with non-aligning times using Julia and Dataframes?


I have multiple time series with non-aligning time values. Plotted below. Simple time-pressure data. How would I compute a difference between a pressure in df1 with that in df2 when the rows have slightly different timestamps and the frames have different shapes (df2 has many more rows)?

simple data plot

If the timestamps aligned between dataframes, and the frames had the same number of rows, I would expect this very simple intermediate column would suffice, but clearly in this case it won't. (Each dataset comes from a different device)

wishfull thinking

How do I generate a new pair of time-pressure columns having some sort of fixed/specific time interval and interpolated values from the original time-pressure columns?

Or is there a more sensible/easier way to achieve what I want to do (essentially, I just want to accurately quantify the differences between "208417" and the other series)?


Solution

  • when the rows have slightly different timestamps a closejoin must used first,e.g.

    using InMemoryDatasets
    
    df1=Dataset(Time=[Date("2022-09-28"),Date("2022-10-02"),Date("2022-10-03"),Date("2022-10-04")],Pressure=[10,11,9,15])
    
    df2=Dataset(Time=[Date("2022-10-01"),Date("2022-10-02"),Date("2022-10-05")],Pressure=[7,9,19])
    
    closejoin!(df1,df2,on=[:Time],makeunique=true,border=:missing)
    
    modify!(df1,[:Pressure,:Pressure_1]=>byrow(-)=>:Difference1)
    
    @df df1 plot(:Time,:Difference1,label="Difference1",color=:red)