Hi,
I have a column in a dataframe which is of type =String15. I need to convert that to float.
julia> typeof(test[!,:Conc])
Vector{String15} (alias for Array{String15, 1})
When I tried to convert the Conc variable to float using the below code, I get this error. Can someone help with the error, please? Not sure what I am missing. Thanks.
test[!,:Conc] = float.(test[!,:Conc])
Error message
ERROR: MethodError: no method matching AbstractFloat(::String15)
Closest candidates are:
(::Type{T})(::SymbolicUtils.Symbolic) where T<:Union{AbstractFloat, Integer, Complex{<:AbstractFloat}, Complex{<:Integer}}
@ Symbolics C:\a\PumasSystemImages\PumasSystemImages\julia_depot\packages\Symbolics\UrqtQ\src\Symbolics.jl:150
(::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number}
@ Base char.jl:50
(::Type{T})(::Base.TwicePrecision) where T<:Number
@ Base twiceprecision.jl:266
...
Stacktrace:
[1] float(x::String15)
@ Base .\float.jl:294
[2] _broadcast_getindex_evalf
@ .\broadcast.jl:683 [inlined]
[3] _broadcast_getindex
@ .\broadcast.jl:656 [inlined]
[4] getindex
@ .\broadcast.jl:610 [inlined]
[5] copy
@ .\broadcast.jl:912 [inlined]
[6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(float), Tuple{Vector{String15}}})
@ Base.Broadcast .\broadcast.jl:873
[7] top-level scope
When you end up with a column like that, itβs often because CSV.read wasnβt able to figure out how to interpret something in a numerical column in the CSV file. Itβs often a bit simpler to adjust the call to CSV.read instead of converting the columns afterwards. An example of a CSV that causes a string column is shown below. If the missing value is coded as NA then weβd have to tell CSV.read. (Just ignore the IOBuffer since itβs just to pass a string to CSV.read instead of a file)
Thanks @vijay and @andreasnoack .
Yes, as you pointed out, it was because of a character in the variable column, which was otherwise numeric entries. The CSV.read with the missingstring took care of the error.
The below code in CSV.read using the typesargument also worked.