in-place operation requires that your original columns accept missing:
julia> df = DataFrame(a = ["a", "b", "?"], b = ["?", "b", "?"])
3Γ2 DataFrame
Row β a b
β String String
ββββββΌββββββββββββββββ
1 β a ?
2 β b b
3 β ? ?
julia> allowmissing!(df) # this is needed for in-place to work
3Γ2 DataFrame
Row β a b
β String? String?
ββββββΌββββββββββββββββββ
1 β a ?
2 β b b
3 β ? ?
julia> @. df = ifelse(df == "?", missing, df)
3Γ2 DataFrame
Row β a b
β String? String?
ββββββΌββββββββββββββββββ
1 β a missing
2 β b b
3 β missing missing
Otherwise you need to replace columns:
julia> df = DataFrame(a = ["a", "b", "?"], b = ["?", "b", "?"])
3Γ2 DataFrame
Row β a b
β String String
ββββββΌββββββββββββββββ
1 β a ?
2 β b b
3 β ? ?
julia> df[!, :] = @. ifelse(df == "?", missing, df)
3Γ2 DataFrame
Row β a b
β String? String?
ββββββΌββββββββββββββββββ
1 β a missing
2 β b b
3 β missing missing
Note that it is not the same as:
julia> df = @. ifelse(df == "?", missing, df)
3Γ2 DataFrame
Row β a b
β String? String?
ββββββΌββββββββββββββββββ
1 β a missing
2 β b b
3 β missing missing
as this allocates a new data frame, while the above writes new columns into an existing data frame.
Can you explain the details behind error MethodError: no method matching similar(::DataFrames.DataFrame, ::Type{Any}), it doesnβt make sense to me.
Does replace() use similar()` under the hood?
Why do you use ifelse() instead of replace() in this situation?
@bkamins Explained above: the original column is of type String, so when you try to add missing, it is converted to a String. Thatβs why he suggested the allowmissing! method to change the type of the columns to accept missing.
julia> df = DataFrame(x = ["am I ?", "? or else", "now ? in the middle"])
3Γ1 DataFrame
Row β x
β String
ββββββΌβββββββββββββββββββββ
1 β am I ?
2 β ? or else
3 β now ? in the middle
julia> replace.(df, "?" => missing)
3Γ1 DataFrame
Row β x
β String
ββββββΌβββββββββββββββββββββββββββ
1 β am I missing
2 β missing or else
3 β now missing in the middle
In short - in this case replace does something completely different than you think it does.