invenia / Wrangling.jl

Wrangle your data into shape. Deals with Columns and Files and Lags and Cattle.
MIT License
1 stars 0 forks source link

Splitting during join of low resolution (hour/day) to high resolution (5min/hour) #9

Open oxinabox opened 4 years ago

oxinabox commented 4 years ago

(for my reference this is feature 13 on initial feature list) Inverse of #8

Sometimes this split should copy the low resolution (e.g. if the data is temperature) other times it should divide up the low resolution (e.g. if the data is load). So need an option for that.

Examples by Sean Lovett:

df = DataFrame(
    time = [Interval(DateTime(2018,1,1) + Day(t), DateTime(2018,1,1) + Day(t+1), true, false) for t in 0:1:3]
)
df.vals = 1:nrow(df)
display(df)
4×2 DataFrame
│ Row │ time                                         │ vals  │
│     │ Interval{DateTime}                           │ Int64 │
├─────┼──────────────────────────────────────────────┼───────┤
│ 1   │ [2018-01-01T00:00:00 .. 2018-01-02T00:00:00) │ 1     │
│ 2   │ [2018-01-02T00:00:00 .. 2018-01-03T00:00:00) │ 2     │
│ 3   │ [2018-01-03T00:00:00 .. 2018-01-04T00:00:00) │ 3     │
│ 4   │ [2018-01-04T00:00:00 .. 2018-01-05T00:00:00) │ 4     │

df_target = DataFrame(
    time = [Interval(DateTime(2018,1,1) + Hour(t), DateTime(2018,1,1) + Hour(t+1), true, false) for t in 0:1:50]
)
display(df_target)
51×1 DataFrame
│ Row │ time                                         │
│     │ Interval{DateTime}                           │
├─────┼──────────────────────────────────────────────┤
│ 1   │ [2018-01-01T00:00:00 .. 2018-01-01T01:00:00) │
│ 2   │ [2018-01-01T01:00:00 .. 2018-01-01T02:00:00) │
│ 3   │ [2018-01-01T02:00:00 .. 2018-01-01T03:00:00) │
│ 4   │ [2018-01-01T03:00:00 .. 2018-01-01T04:00:00) │
│ 5   │ [2018-01-01T04:00:00 .. 2018-01-01T05:00:00) │
│ 6   │ [2018-01-01T05:00:00 .. 2018-01-01T06:00:00) │
│ 7   │ [2018-01-01T06:00:00 .. 2018-01-01T07:00:00) │
│ 8   │ [2018-01-01T07:00:00 .. 2018-01-01T08:00:00) │
│ 9   │ [2018-01-01T08:00:00 .. 2018-01-01T09:00:00) │
│ 10  │ [2018-01-01T09:00:00 .. 2018-01-01T10:00:00) │
│ 11  │ [2018-01-01T10:00:00 .. 2018-01-01T11:00:00) │
│ 12  │ [2018-01-01T11:00:00 .. 2018-01-01T12:00:00) │
│ 13  │ [2018-01-01T12:00:00 .. 2018-01-01T13:00:00) │

df_deagg = DataFrame(df_target)
df_deagg.vals = zeros(nrow(df_agg))
for i in 1:nrow(df_deagg)
    # Copy to all the deaggregatated pouints
    # Here is where we would also use a option to divide them up
    df_deagg.vals[i] = only(@where(df, issubset.(df_deagg.time[i], :time)).vals)
end

julia> display(df_deagg)
51×2 DataFrame
│ Row │ time                                         │ vals    │
│     │ Interval{DateTime}                           │ Float64 │
├─────┼──────────────────────────────────────────────┼─────────┤
│ 1   │ [2018-01-01T00:00:00 .. 2018-01-01T01:00:00) │ 1.0     │
│ 2   │ [2018-01-01T01:00:00 .. 2018-01-01T02:00:00) │ 1.0     │
│ 3   │ [2018-01-01T02:00:00 .. 2018-01-01T03:00:00) │ 1.0     │
│ 4   │ [2018-01-01T03:00:00 .. 2018-01-01T04:00:00) │ 1.0     │
│ 5   │ [2018-01-01T04:00:00 .. 2018-01-01T05:00:00) │ 1.0     │
│ 6   │ [2018-01-01T05:00:00 .. 2018-01-01T06:00:00) │ 1.0     │
│ 7   │ [2018-01-01T06:00:00 .. 2018-01-01T07:00:00) │ 1.0     │
│ 8   │ [2018-01-01T07:00:00 .. 2018-01-01T08:00:00) │ 1.0     │
│ 9   │ [2018-01-01T08:00:00 .. 2018-01-01T09:00:00) │ 1.0     │
│ 10  │ [2018-01-01T09:00:00 .. 2018-01-01T10:00:00) │ 1.0     │
│ 11  │ [2018-01-01T10:00:00 .. 2018-01-01T11:00:00) │ 1.0     │
│ 12  │ [2018-01-01T11:00:00 .. 2018-01-01T12:00:00) │ 1.0     │
│ 13  │ [2018-01-01T12:00:00 .. 2018-01-01T13:00:00) │ 1.0     │
│ 14  │ [2018-01-01T13:00:00 .. 2018-01-01T14:00:00) │ 1.0     │
│ 15  │ [2018-01-01T14:00:00 .. 2018-01-01T15:00:00) │ 1.0     │
│ 16  │ [2018-01-01T15:00:00 .. 2018-01-01T16:00:00) │ 1.0     │
│ 17  │ [2018-01-01T16:00:00 .. 2018-01-01T17:00:00) │ 1.0     │
│ 18  │ [2018-01-01T17:00:00 .. 2018-01-01T18:00:00) │ 1.0     │
│ 19  │ [2018-01-01T18:00:00 .. 2018-01-01T19:00:00) │ 1.0     │
│ 20  │ [2018-01-01T19:00:00 .. 2018-01-01T20:00:00) │ 1.0     │
│ 21  │ [2018-01-01T20:00:00 .. 2018-01-01T21:00:00) │ 1.0     │
│ 22  │ [2018-01-01T21:00:00 .. 2018-01-01T22:00:00) │ 1.0     │
│ 23  │ [2018-01-01T22:00:00 .. 2018-01-01T23:00:00) │ 1.0     │
│ 24  │ [2018-01-01T23:00:00 .. 2018-01-02T00:00:00) │ 1.0     │
│ 25  │ [2018-01-02T00:00:00 .. 2018-01-02T01:00:00) │ 2.0     │
│ 26  │ [2018-01-02T01:00:00 .. 2018-01-02T02:00:00) │ 2.0     │
│ 27  │ [2018-01-02T02:00:00 .. 2018-01-02T03:00:00) │ 2.0     │
│ 28  │ [2018-01-02T03:00:00 .. 2018-01-02T04:00:00) │ 2.0     │
│ 29  │ [2018-01-02T04:00:00 .. 2018-01-02T05:00:00) │ 2.0     │
│ 30  │ [2018-01-02T05:00:00 .. 2018-01-02T06:00:00) │ 2.0     │
│ 31  │ [2018-01-02T06:00:00 .. 2018-01-02T07:00:00) │ 2.0     │
│ 32  │ [2018-01-02T07:00:00 .. 2018-01-02T08:00:00) │ 2.0     │
│ 33  │ [2018-01-02T08:00:00 .. 2018-01-02T09:00:00) │ 2.0     │
│ 34  │ [2018-01-02T09:00:00 .. 2018-01-02T10:00:00) │ 2.0     │
│ 35  │ [2018-01-02T10:00:00 .. 2018-01-02T11:00:00) │ 2.0     │
│ 36  │ [2018-01-02T11:00:00 .. 2018-01-02T12:00:00) │ 2.0     │
│ 37  │ [2018-01-02T12:00:00 .. 2018-01-02T13:00:00) │ 2.0     │
│ 38  │ [2018-01-02T13:00:00 .. 2018-01-02T14:00:00) │ 2.0     │
│ 39  │ [2018-01-02T14:00:00 .. 2018-01-02T15:00:00) │ 2.0     │
│ 40  │ [2018-01-02T15:00:00 .. 2018-01-02T16:00:00) │ 2.0     │
│ 41  │ [2018-01-02T16:00:00 .. 2018-01-02T17:00:00) │ 2.0     │
│ 42  │ [2018-01-02T17:00:00 .. 2018-01-02T18:00:00) │ 2.0     │
│ 43  │ [2018-01-02T18:00:00 .. 2018-01-02T19:00:00) │ 2.0     │
│ 44  │ [2018-01-02T19:00:00 .. 2018-01-02T20:00:00) │ 2.0     │
│ 45  │ [2018-01-02T20:00:00 .. 2018-01-02T21:00:00) │ 2.0     │
│ 46  │ [2018-01-02T21:00:00 .. 2018-01-02T22:00:00) │ 2.0     │
│ 47  │ [2018-01-02T22:00:00 .. 2018-01-02T23:00:00) │ 2.0     │
│ 48  │ [2018-01-02T23:00:00 .. 2018-01-03T00:00:00) │ 2.0     │
│ 49  │ [2018-01-03T00:00:00 .. 2018-01-03T01:00:00) │ 3.0     │
│ 50  │ [2018-01-03T01:00:00 .. 2018-01-03T02:00:00) │ 3.0     │
│ 51  │ [2018-01-03T02:00:00 .. 2018-01-03T03:00:00) │ 3.0     │

As per #8, need to make sure handles inclusivity and timezones right (or alterntative make sure it errors if they are not the easy case) Similar for partial overlaps

This is another use case for Conditional Joins.