dear nalimilanβunfortunately, these were not the problems.
using DataFrames ## includes Missing
( XFloat64= eltype([ 1.0, missing ]);
VXFloat64= Vector{XFloat64}; VFloat64= Vector{Float64}; )
function nan2missing!(x::VXFloat64)::VXFloat64
x= allowmissing(x) ## random attempt; makes no difference
ifelse.( isnan.(x), missing, x )
end#function nan2missing!(VXFloat64)
function nan2missing!(x::VFloat64)::VXFloat64
nan2missing!(convert(VXFloat64, x))
end#function nan2missing!(VFloat64)
println( "plain vector works: ", nan2missing!( [ 1.0, NaN, 2.0] ) )
println( "recode works: ", recode( [1.0, NaN, missing, 4.0], NaN => missing ))
try
println( "union-vector works: ", nan2missing!( [ 1.0, NaN, missing, 3.0 ]) )
catch ; println("union-vector fails ") ; end
df= DataFrame( n1=[ 1,2,3,missing ], n2=[ 1.0,2.0,3.0,missing ],
n3=[ 1.0, NaN, 2.0, missing ], n4=[ NaN, 2.0, 3.0, 4.0 ] )
function nan2missing!(din::DataFrames.DataFrame)::DataFrames.DataFrame
for col in eachcol(din)
if (eltype(col) <: Float64)
df[Symbol(col)]= recode(df[Symbol(col)], NaN => missing)
## or v=allowmissing(df[Symbol(col)]); v[isnan.(v)= missing; df[Symbol(col)]= v
end#if
end#for
din
end#function nan2missing!(Dataframes)
println(nan2missing!(df)) ## return value
println(df) ## altered value
(any(isnan.(df[ :n3 ]))) && println("NaNs were not replaced.")
relevant output is
plain vector works: Union{Float64, Missings.Missing}[1.0, missing, 2.0]
recode works: Union{Float64, Missings.Missing}[1.0, missing, missing, 4.0]
union-vector fails
4Γ4 DataFrames.DataFrame
β Row β n1 β n2 β n3 β n4 β
βββββββΌββββββββββΌββββββββββΌββββββββββΌββββββ€
β 1 β 1 β 1.0 β 1.0 β NaN β
β 2 β 2 β 2.0 β NaN β 2.0 β
β 3 β 3 β 3.0 β 2.0 β 3.0 β
β 4 β missing β missing β missing β 4.0 β
NaNs were not replaced.