I have some temporal experimental data in a DataFrame. I would like to round the timestamps to the nearest 10 seconds and then drop any rows that are not on an even minute. I have everything but the last step working below.
df = DataFrame()
df.Time = ["11:05:02", "11:05:23", "11:05:34", "11:05:42", "11:06:01"]
df.Data = rand(5)
df.Time = DateTime.(df.Time, DateFormat("H:M:S")) # I would rather just use Time type
df.Time = round.(df.Time, Second(10)) # but round is only defined for DateTime type
julia> df
5×2 DataFrame
Row │ Time Data
│ DateTime Float64
─────┼───────────────────────────────
1 │ 0001-01-01T11:05:00 0.987827
2 │ 0001-01-01T11:05:20 0.534373
3 │ 0001-01-01T11:05:30 0.571214
4 │ 0001-01-01T11:05:40 0.306041
5 │ 0001-01-01T11:06:00 0.209411
julia> filter!(:Time => time -> time % Minute(1) == 0, df)
ERROR: MethodError: no method matching rem(::DateTime, ::Minute)
Regarding rounding you could always define a simple function such as:
function round10(t::Time)
s= second(t) % 10
s <=5 && return t - Second(s)
return t + Second(10-s)
end
This will allow to store times as Time that is df.Time = Time.(df.Time, DateFormat("H:M:S")).
Selecting rows that are on an even minute after rounding could be accomplished as
julia> df[minute.(round10.(df.Time)) .% 2 .== 0, :]
1×2 DataFrame
│ Row │ Time │ Data │
│ │ Time │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 11:06:01 │ 0.537324 │
or
julia> filter!(:Time => t -> minute(round10(t)) % 2 == 0, df)
1×2 DataFrame
│ Row │ Time │ Data │
│ │ Time │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 11:06:01 │ 0.537324 │
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With