TypeError: non-boolean (Missing) used in boolean context

I try to run this code

## remove NaNs
df_food[:gpa] =
  collect(FltOrMiss, map(x -> isnan(x)?missing:x, df_food[:gpa]))
df_food[:wgt] =
collect(FltOrMiss, map(x -> isnan(x)?missing:x, df_food[:wgt]))

But get this error:

TypeError: non-boolean (Missing) used in boolean context

Stacktrace:
 [1] #40 at ./In[31]:1 [inlined]
 [2] iterate at ./generator.jl:47 [inlined]
 [3] collect_to!(::Array{Float64,1}, ::Base.Generator{Array{Union{Missing, Float64},1},getfield(Main, Symbol("##40#41"))}, ::Int64, ::Int64) at ./array.jl:656
 [4] collect_to_with_first! at ./array.jl:643 [inlined]
 [5] _collect(::Array{Union{Missing, Float64},1}, ::Base.Generator{Array{Union{Missing, Float64},1},getfield(Main, Symbol("##40#41"))}, ::Base.EltypeUnknown, ::Base.HasShape{1}) at ./array.jl:637
 [6] collect_similar(::Array{Union{Missing, Float64},1}, ::Base.Generator{Array{Union{Missing, Float64},1},getfield(Main, Symbol("##40#41"))}) at ./array.jl:561
 [7] map(::Function, ::Array{Union{Missing, Float64},1}) at ./abstractarray.jl:1987
 [8] top-level scope at In[31]:1

How can i solve this unexpected error.

Is it possible that some elements from df_food[:gpa] are missing?
In that case isnan(x) will return missing (a non-boolean).
Maybe you need to also test for ismissing(x)

2 Likes

yes, it works, thanks

How did you fix the problem?

1 Like