Julia Dataframes - concisely create column with eltype Union{Missing, T}
Solution 1:
This is a way to do it (there are other options how to add a column to a data frame, but the key function to use is missings
):
julia> using DataFrames
julia> df = DataFrame()
0×0 DataFrame
julia> df.col = missings(Int, 5)
5-element Vector{Union{Missing, Int64}}:
missing
missing
missing
missing
missing
julia> df
5×1 DataFrame
Row │ col
│ Int64?
─────┼─────────
1 │ missing
2 │ missing
3 │ missing
4 │ missing
5 │ missing
julia> df.other_col = missings(Float64, nrow(df))
5-element Vector{Union{Missing, Float64}}:
missing
missing
missing
missing
missing
julia> df
5×2 DataFrame
Row │ col other_col
│ Int64? Float64?
─────┼────────────────────
1 │ missing missing
2 │ missing missing
3 │ missing missing
4 │ missing missing
5 │ missing missing
As a side note - this issue is unrelated with DataFrames.jl but related to how vectors are created in Julia in general. The missings
function is defined in the Missings.jl package (that is re-exported by DataFrames.jl). If you wanted to use Julia Base functionality only then the following would give you the same as using missings
:
julia> Vector{Union{Int, Missing}}(missing, 5)
5-element Vector{Union{Missing, Int64}}:
missing
missing
missing
missing
missing
(however, since it is more verbose I typically use the missings
function)