What is an idiomatic way how to fill gaps in DataFrame
? “Gaps” for me are some unknown data. One example is - I need to draw a line, but I have values only for some points. I need to supply some values (e.g. zero) for those X points.
Let’s say I have something like this:
using Dates
df = DataFrame(Date = [Date(2021, 05, 07), Date(2021, 05, 09), Date(2021, 05, 11)], Price = [10, 13, 8], Quantity=[1049, 1145, 901])
allDates = Date(2021, 05, 06):Dates.Day(1):Date(2021, 05, 11)
And I’d like to have DataFrame
that contains all the dates from allDates
. And if the date is not from original df
, then Price
and Quantity
is 0
.
Wanted result:
julia> df = DataFrame(
Date = [Date(2021,05,06), Date(2021,05,07), Date(2021,05,08), Date(2021,05,09), Date(2021,05,10), Date(2021,05,11)],
Price = [0, 10, 0, 13, 0, 8],
Quantity = [0, 1049, 0, 1145, 0, 901])
6×3 DataFrame
Row │ Date Price Quantity
│ Date Int64 Int64
─────┼─────────────────────────────
1 │ 2021-05-06 0 0
2 │ 2021-05-07 10 1049
3 │ 2021-05-08 0 0
4 │ 2021-05-09 13 1145
5 │ 2021-05-10 0 0
6 │ 2021-05-11 8 901
I can create a DataFrame
in a loop row by row, but that’s probably not the easiest way…
What is the best approach here, please?