Julia, or more specifically broadcasting, was not successfully informed that the column type should change.
julia> df[!, "C"]
3-element Vector{Missing}:
missing
missing
missing
julia> convert.(Union{Float64, Missing}, df[!, "C"])
3-element Vector{Missing}:
missing
missing
missing
The convert.(...)
line is equivalent to broadcast(convert, ...)
, and that infers the output array type from the elementwise convert
. Since all the missing
are converted to missing
, it inferred the output array type is Missing
.
You can instantiate a vector with your intended element type, and while there is in-place broadcasting, in this case you can directly push values from another vector:
julia> append!(Union{Float64, Missing}[], df[!, "C"])
3-element Vector{Union{Missing, Float64}}:
missing
missing
missing
There is in fact a convert
method for arrays that does this for you, and this one just forwards to the Array
constructor that takes an Array
input:
julia> convert(Vector{Union{Float64, Missing}}, df[!, "C"])
3-element Vector{Union{Missing, Float64}}:
missing
missing
missing
julia> Vector{Union{Float64, Missing}}(df[!, "C"])
3-element Vector{Union{Missing, Float64}}:
missing
missing
missing
Now that will work for the DataFrame
:
julia> df[!, "C"] = convert(Vector{Union{Float64, Missing}}, df[!, "C"])
3-element Vector{Union{Missing, Float64}}:
missing
missing
missing
julia> df
3×3 DataFrame
Row │ A B C
│ Int64 String Float64?
─────┼─────────────────────────
1 │ 1 a missing
2 │ 2 b missing
3 │ 3 c missing
julia> df[1,"C"] = 4.6
4.6
You likely want to make a similar adjustment at the instantiation of df
so you don’t have a Vector{Missing}
in the first place, but I can’t comment on what isn’t shown. In the future, share your code as text that readers can copy and run successfully. This dataframe was simple enough to reproduce from the screenshot, but that won’t usually be the case so the minimal working examples help a lot.