According to allowmissing!
in DataFrames.jl,
it is possible to make a DataFrame “able to receive missing”:
julia> using DataFrames
d
julia> df = DataFrame()
0×0 DataFrame
julia> push!(df, (;a=1, b=1.0))
1×2 DataFrame
Row │ a b
│ Int64 Float64
─────┼────────────────
1 │ 1 1.0
julia> push!(df, (;a=1, b=missing))
┌ Error: Error adding value to column :b.
└ @ DataFrames /Users/jinrae/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:1483
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Float64
Closest candidates are:
convert(::Type{T}, ::T) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:6
convert(::Type{T}, ::Number) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:7
convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/twiceprecision.jl:262
...
Stacktrace:
[1] push!(a::Vector{Float64}, item::Missing)
@ Base ./array.jl:994
[2] push!(df::DataFrame, row::NamedTuple{(:a, :b), Tuple{Int64, Missing}}; cols::Symbol, promote::Bool)
@ DataFrames ~/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:1465
[3] push!(df::DataFrame, row::NamedTuple{(:a, :b), Tuple{Int64, Missing}})
@ DataFrames ~/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:1353
[4] top-level scope
@ REPL[4]:1
julia> allowmissing!(df)
1×2 DataFrame
Row │ a b
│ Int64? Float64?
─────┼──────────────────
1 │ 1 1.0
julia> push!(df, (;a=1, b=missing))
2×2 DataFrame
Row │ a b
│ Int64? Float64?
─────┼───────────────────
1 │ 1 1.0
2 │ 1 missing
However, I don’t get it how to initially allow a DataFrame receivable to missing data.
For example, the following things didn’t work.
julia> using DataFrames
julia> df = DataFrame()
al0×0 DataFrame
julia> allowmissing!(df)
0×0 DataFrame
julia> push!(df, (;a=1, b=1.0))
pu1×2 DataFrame
Row │ a b
│ Int64 Float64
─────┼────────────────
1 │ 1 1.0
julia> push!(df, (;a=1, b=missing))
┌ Error: Error adding value to column :b.
└ @ DataFrames /Users/jinrae/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:1483
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Float64
Closest candidates are:
convert(::Type{T}, ::T) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:6
convert(::Type{T}, ::Number) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:7
convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/twiceprecision.jl:262
...
Stacktrace:
[1] push!(a::Vector{Float64}, item::Missing)
@ Base ./array.jl:994
[2] push!(df::DataFrame, row::NamedTuple{(:a, :b), Tuple{Int64, Missing}}; cols::Symbol, promote::Bool)
@ DataFrames ~/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:1465
[3] push!(df::DataFrame, row::NamedTuple{(:a, :b), Tuple{Int64, Missing}})
@ DataFrames ~/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:1353
[4] top-level scope
@ REPL[5]:1