I am trying to figure out how to pre-allocate an array with a custom struct with zeroed values. I figured out how to allocate using undef, but I am uncertain how I can do that while setting the values other than a loop. It feels like I am missing something.
Method that works
using DataFrames, Dates
struct MyData
rev::Float64
qty::Int64
MyData() = new(0,0)
end
arr =[]
for i in 1:10
push!(arr,MyData())
end
time_series = DataFrame()
for date in Date(2019,1):Dates.Month(1):Date(2020,12)
time_series[Symbol(date)] = arr
end
ts
10Ć24 DataFrame. Omitted printing of 17 columns
ā Row ā 2019-01-01 ā 2019-02-01 ā 2019-03-01 ā 2019-04-01 ā 2019-05-01 ā 2019-06-01 ā 2019-07-01 ā
ā ā Any ā Any ā Any ā Any ā Any ā Any ā Any ā
āāāāāāāāā¼āāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā 1 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
ā 2 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
ā 3 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
ā 4 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
ā 5 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
ā 6 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
ā 7 ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā MyData(0.0, 0) ā
Thing I sort of wish I could do
for date in Date(2019,1):Dates.Month(1):Date(2020,12)
time_series[Symbol(date)] = Array{MyData}(ZeroSomehow,100)
end
Am I missing a more Julian way of getting this done? I checked the source for answers, but I was unable to figure out much from UndefInitializer. I am not good at digging through it yet.