Reputation: 23
Trying to replace values of 99 to missing:
df_miss = DataFrame(a=[1,2,99,99,5,6,99])
allowmissing!(df_miss)
df_miss.a .= replace.(df_miss.a, 99 => missing)
But getting this error:
MethodError: no method matching similar(::Int64, ::Type{Union{Missing, Int64}})
Using:
Julia Version 1.5.3
DataFrames Version 0.22.7
Upvotes: 2
Views: 186
Reputation: 69819
You do not need to use broadcasting here. Just write:
julia> df_miss = DataFrame(a=[1,2,99,99,5,6,99])
7×1 DataFrame
Row │ a
│ Int64
─────┼───────
1 │ 1
2 │ 2
3 │ 99
4 │ 99
5 │ 5
6 │ 6
7 │ 99
julia> df_miss.a = replace(df_miss.a, 99 => missing)
7-element Vector{Union{Missing, Int64}}:
1
2
missing
missing
5
6
missing
julia> df_miss
7×1 DataFrame
Row │ a
│ Int64?
─────┼─────────
1 │ 1
2 │ 2
3 │ missing
4 │ missing
5 │ 5
6 │ 6
7 │ missing
Upvotes: 3