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 5 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
20 changes: 16 additions & 4 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,26 @@ 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
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)
return .!ismissing.(v)
else
return trues(size(df, 1))
end
end

completecases(df::AbstractDataFrame, cols::MultiColumnIndex) =
completecases(df[!, cols])
Expand Down
14 changes: 12 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 @@ -128,6 +137,7 @@ end

@test_throws ArgumentError completecases(DataFrame())
@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