Not able convert string to missing

Hi,

I have a string variable in my dataframe. Im tryng to convert that to float 64. below is the process and problems im faced
first i tried to convert string varaible to float64. Below is the error.

phen7[!,:ALB] = passmissing(parse).(Float64,phen7[!,:ALB])
ERROR: ArgumentError: cannot parse "" as Float64
Stacktrace:
  [1] _parse_failure(T::Type, s::String, startpos::Int64, endpos::Int64) (repeats 2 times)
    @ Base ./parse.jl:373
  [2] #tryparse_internal#452
    @ ./parse.jl:369 [inlined]
  [3] tryparse_internal
    @ ./parse.jl:367 [inlined]
  [4] #parse#453
    @ ./parse.jl:379 [inlined]
  [5] parse
    @ ./parse.jl:379 [inlined]
  [6] macro expansion
    @ ~/.julia/packages/Missings/r1STI/src/Missings.jl:0 [inlined]
  [7] PassMissing
    @ ~/.julia/packages/Missings/r1STI/src/Missings.jl:190 [inlined]
  [8] _broadcast_getindex_evalf
    @ ./broadcast.jl:670 [inlined]
  [9] _broadcast_getindex
    @ ./broadcast.jl:653 [inlined]
 [10] getindex
    @ ./broadcast.jl:597 [inlined]
 [11] macro expansion
    @ ./broadcast.jl:961 [inlined]
 [12] macro expansion
    @ ./simdloop.jl:77 [inlined]
 [13] copyto!
    @ ./broadcast.jl:960 [inlined]
 [14] copyto!
    @ ./broadcast.jl:913 [inlined]
 [15] copy
    @ ./broadcast.jl:885 [inlined]
 [16] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, Missings.PassMissing{typeof(parse)}, Tuple{Base.RefValue{Type{Float64}}, Vector{String}}})
    @ Base.Broadcast ./broadcast.jl:860
 [17] top-level scope
    @ none:1

Then i tried to replace “” to missing, Below is the error.

julia> replace!(phen7.ALB, "" => missing)
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type String
Closest candidates are:
  convert(::Type{T}, ::P) where {T, P<:(Polynomials.AbstractPolynomial{T})} at ~/.julia/packages/Polynomials/DglHf/src/common.jl:433
  convert(::Type{String}, ::FilePathsBase.AbstractPath) at ~/.julia/packages/FilePathsBase/qgXdE/src/path.jl:117
  convert(::Type{String}, ::WeakRefStrings.WeakRefString) at ~/.julia/packages/WeakRefStrings/31nkb/src/WeakRefStrings.jl:81
  ...
Stacktrace:
 [1] setindex!(A::Vector{String}, x::Missing, i1::Int64)
   @ Base ./array.jl:903
 [2] _replace!(new::Base.var"#new#329"{Tuple{Pair{String, Missing}}}, res::Vector{String}, A::Vector{String}, count::Int64)
   @ Base ./set.jl:681
 [3] replace_pairs!
   @ ./set.jl:499 [inlined]
 [4] #replace!#328
   @ ./set.jl:489 [inlined]
 [5] replace!(A::Vector{String}, old_new::Pair{String, Missing})
   @ Base ./set.jl:489
 [6] top-level scope
   @ none:1

this is how my values look

julia> println(unique(phen7.ALB))
["", "3.5", "4.5", "2.2", "3.8", "2.9", "3.6", "4.0", "4.6", "1.8", "2.1", "3.9", "4.4", "3.4", "3.7", "4.1", "4.3", "4.2", "4.8", "2.5", "3.3", "3.2", "3.1", "2.3", "2.7", "3.0", "2.6", "2.8", "2.4", "1.7", "2.0", "1.9", "<1.5", "5.0", "5.1"]

Can some one help me to convert this at Float64.

Thanks

Use replace rather than replace! - your values are stored in a Vector{String} which can’t hold values of type Missing.

So you want parse.(Float64, replace(phen7.ALB, "" => missing)) but note that this will still fail as one of your values is "<1.5" which can’t be parsed as float - you’ll have to decide what number you want to assign to this.

1 Like

Thank you, i will assign another value for “<1.5”