I would like to set some values across a whole datafame using a boolean mask, similarly to this example with plain arrays:
julia> x = rand(3, 3)
3×3 Matrix{Float64}:
0.0559191 0.249678 0.165318
0.845124 0.83681 0.248268
0.291518 0.0380153 0.205101
julia> x[x .< 0.5] .= 0;
julia> x
3×3 Matrix{Float64}:
0.0 0.0 0.0
0.845124 0.83681 0.0
0.0 0.0 0.0
It turns out that this pattern is not allowed with dataframes though:
julia> df = DataFrame(rand(3,3), :auto)
3×3 DataFrame
Row │ x1 x2 x3
│ Float64 Float64 Float64
─────┼───────────────────────────────
1 │ 0.045519 0.468771 0.387336
2 │ 0.0133922 0.383619 0.418809
3 │ 0.870746 0.898979 0.628106
julia> df .< 0.5
3×3 DataFrame
Row │ x1 x2 x3
│ Bool Bool Bool
─────┼─────────────────────
1 │ true true true
2 │ true true true
3 │ false false false
julia> df[df .< 0.5] .= 0
ERROR: MethodError: no method matching getindex(::DataFrame, ::DataFrame)
Closest candidates are:
getindex(::AbstractDataFrame, ::CartesianIndex{2}) at ~/.julia/packages/DataFrames/zqFGs/src/other/broadcasting.jl:3
getindex(::AbstractDataFrame, ::Integer, ::Colon) at ~/.julia/packages/DataFrames/zqFGs/src/dataframerow/dataframerow.jl:210
getindex(::AbstractDataFrame, ::Integer, ::Union{Colon, Regex, AbstractVector, All, Between, Cols, InvertedIndex}) at ~/.julia/packages/DataFrames/zqFGs/src/dataframerow/dataframerow.jl:208
...
Stacktrace:
[1] maybeview
@ ./views.jl:145 [inlined]
[2] dotview(::DataFrame, ::DataFrame)
@ Base.Broadcast ./broadcast.jl:1200
[3] top-level scope
@ REPL[39]:1
What is an alternative syntax?