How to add a row to DataFrame where one field is missing

The MWE below tries to add a row to a DataFrame where some rows have missing values:

tps = [String,Int64,Int64]
nms = ["str","int","int2"]
df = DataFrame(tps,nms)
data = [(str="a",int=1,int2=missing),(str="b",int=2,int2=0)]
function loaddata(data,df)
    for d in data
        push!(df,d)
    end
end

loaddata(data,df)
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Int64

use promote = true as a keyword argument.

The issue was initializing the DataFrame with Union types to allow missing values in the rows. This works:

tps = [Union{Missing,String},Union{Missing,Int64},Union{Missing,Int64}]
nms = ["str","int","int2"]
df = DataFrame(tps,nms)
data = [(str="a",int=1,int2=missing),(str="b",int=2,int2=0)]
function loaddata(data,df)
    for d in data
        push!(df,d)
    end
end

loaddata(data,df)

Thanks, @pdeffebach. Sorry I wasn’t clear, I don’t want to create new columns so I can’t use promote

ah sorry, you want cols = :union as a keyword argument. check out the docs with ?push! and scroll down to the section on data frames.

1 Like