I have a dataframe with quite a few columns of type Int8 that I need to convert to Int64 to satisfy XLSX.jl non-support for integer sizes other than Int64.
Currently I do it column by column:
sf[!,:Enumeration] = convert.(Int64, sf[:, :Enumeration]) #needed by XLSX
There must be a better way, but I am too much of a novice to figure it out.
You can find the names of the columns that are Int8
using names(df, Int8)
.
ulia> using DataFrames
julia> df = DataFrame(a = rand(1:10, 5), b = randn(5), c = Int8.(rand(11:20, 5)))
5×3 DataFrame
Row │ a b c
│ Int64 Float64 Int8
─────┼─────────────────────────
1 │ 10 0.0594745 17
2 │ 5 -2.09879 12
3 │ 10 -0.765042 17
4 │ 6 -0.258906 12
5 │ 7 -0.721766 18
julia> names(df, Int8)
1-element Vector{String}:
"c"
1 Like
First of all, welcome to our community!
Complementing the suggestion of @dmbates, use names
and then iterate over it:
for int8_col_name in names(sf, Int8)
sf[!, int8_col_name] = convert.(Int64, sf[:, int8_col_name])
end
This is even more concise:
transform!(sf, names(sf, Int8) .=> ByRow(Int), renamecols=false)
3 Likes