Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize completecases to process only missingable columns #2726

Merged
merged 7 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,28 @@ function completecases(df::AbstractDataFrame, col::Colon=:)
"data frame with no columns"))
end
res = trues(size(df, 1))
aux = BitVector(undef, size(df, 1))
for i in 1:size(df, 2)
res .&= .!ismissing.(df[!, i])
v = df[!, i]
if Missing <: eltype(v)
# Disable fused broadcasting as it happens to be much slower
aux .= .!ismissing.(v)
res .&= aux
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end
end
res
return res
end

completecases(df::AbstractDataFrame, col::ColumnIndex) =
.!ismissing.(df[!, col])
function completecases(df::AbstractDataFrame, col::ColumnIndex)
v = df[!, col]
if Missing <: eltype(v)
res = BitVector(undef, size(df, 1))
res .= .!ismissing.(v)
return res
Comment on lines +779 to +781
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just this?

Suggested change
res = BitVector(undef, size(df, 1))
res .= .!ismissing.(v)
return res
return .!ismissing.(v)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it is not type stable. I have just reversed this. The current design (with res) has no performance penalty, but pasess @inferred.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's surprising. How about just adding ::BitVector in the function definition? That would make it clearer that the goal is to fix inference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you would ask about it :).

The ::BitVector annotation could potentially allocate once more by converting Vector{Bool} to BitVector while what I do should guarantee only that only one allocation happens because broadcasting assignment does copyto! of Broadcasted into target.

Here is an example:

julia> f1(x::AbstractVector)::BitVector = .!ismissing.(x)
f1 (generic function with 1 method)

julia> f2(x::AbstractVector) = (res = BitVector(undef, length(x)); res .= .!ismissing.(x); return x)
f2 (generic function with 1 method)

julia> using SparseArrays

julia> x = sparse(1:10^7);

julia> using BenchmarkTools

julia> f1(x::AbstractVector)::BitVector = .!ismissing.(x)
f1 (generic function with 1 method)

julia> @btime f1($x);
  568.808 ms (7 allocations: 87.02 MiB)

julia> @btime f2($x);
  525.133 ms (4 allocations: 1.20 MiB)

else
return trues(size(df, 1))
end
end

completecases(df::AbstractDataFrame, cols::MultiColumnIndex) =
completecases(df[!, cols])
Expand Down
15 changes: 13 additions & 2 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,17 @@ end
:auto)
df2 = DataFrame([Union{Int, Missing}[1, 2, 3, 4], ["one", "two", missing, "four"]],
:auto)

@test df2[completecases(df2), :] == df2[[1, 2, 4], :]
df3 = DataFrame(x = Int[1, 2, 3, 4], y = Union{Int, Missing}[1, missing, 2, 3],
z = Missing[missing, missing, missing, missing])

@test completecases(df2) == .!ismissing.(df2.x2)
@test @inferred(completecases(df3, :x)) == trues(nrow(df3))
@test completecases(df3, :y) == .!ismissing.(df3.y)
@test completecases(df3, :z) == completecases(df3, [:z, :x]) ==
completecases(df3, [:x, :z]) == completecases(df3, [:y, :x, :z]) ==
falses(nrow(df3))
@test @inferred(completecases(df3, [:y, :x])) ==
completecases(df3, [:x, :y]) == .!ismissing.(df3.y)
@test dropmissing(df2) == df2[[1, 2, 4], :]
returned = dropmissing(df1)
@test df1 == returned && df1 !== returned
Expand All @@ -127,7 +136,9 @@ end
@test df1b == df1

@test_throws ArgumentError completecases(DataFrame())
bkamins marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError completecases(DataFrame(x=1:3), Cols())
@test_throws MethodError completecases(DataFrame(x=1), true)
@test_throws ArgumentError completecases(df3, :a)

for cols in (:x2, "x2", [:x2], ["x2"], [:x1, :x2], ["x1", "x2"], 2, [2], 1:2,
[true, true], [false, true], :,
Expand Down