Add a column of ints to a DataFrame of strings

How to add an int column to a DataFrame of strings?

d = DataFrames.DataFrame([["hello", "world"]], [:strs])
d[:num] = [1,2]

gives an error:

MethodError: Cannot convert an object of type Int64 to an object of type String
This may have arisen from a call to the constructor String(…),
since type constructors fall back to convert methods.

But this works:

d[:num] = ["a","b"]

I thought columns of DataFrame need not have the same type.

https://github.com/JuliaStats/DataFrames.jl/blob/ff8cd116c59dc64f45ff73f67c605bb5c66fd071/src/dataframe/dataframe.jl#L73 should perhaps be Vector{Any}.

1 Like

I made a PR that should fix this but not sure if it has performance problems: https://github.com/JuliaStats/DataFrames.jl/pull/1201