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

Run findall(rows) only if rows are not all true #2727

Merged
merged 17 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions src/other/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,11 @@ function _nt_like_hash(v, h::UInt)

return xor(objectid(Tuple(propertynames(v))), h)
end

function _findall(v::AbstractVector{Bool})
if all(v)
return keys(v)
else
return findall(v)
end
end
2 changes: 1 addition & 1 deletion src/subdataframe/subdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Base.@propagate_inbounds function SubDataFrame(parent::DataFrame, rows::Abstract
throw(ArgumentError("invalid length of `AbstractVector{Bool}` row index " *
"(got $(length(rows)), expected $(nrow(parent)))"))
end
return SubDataFrame(parent, findall(rows), cols)
return SubDataFrame(parent, _findall(rows), cols)
Copy link
Member

Choose a reason for hiding this comment

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

Can you also test performance and corrected of view (it should be OK, but just to be sure as this is the point of the change we make so I prefer to double check). Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll check it with some more scenario than in the beginning of this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fortunately it is faster :)

df = DataFrame(
    a = rand(1000000),
    b = allowmissing(rand(1000000)),
    c = [allowmissing(rand(1000000 - 1)); missing],
    d = repeat([1:9; missing], 100000),
    e = repeat([rand(50000); missings(50000)], 10),
    f = [missings(300000); ones(400000); missings(300000)],
    g = missings(1000000)
)

Main:

julia> @btime dropmissing(df, :a, view=true);
  1.872 ms (9 allocations: 7.75 MiB)

julia> @btime dropmissing(df, :b, view=true);
  2.075 ms (14 allocations: 7.75 MiB)

julia> @btime dropmissing(df, :c, view=true);
  2.111 ms (14 allocations: 7.75 MiB)

julia> @btime dropmissing(df, :d, view=true);
  1.913 ms (14 allocations: 6.99 MiB)

julia> @btime dropmissing(df, :e, view=true);
  1.117 ms (14 allocations: 3.94 MiB)

julia> @btime dropmissing(df, :f, view=true);
  909.713 μs (14 allocations: 3.18 MiB)
 
julia> @btime dropmissing(df, :g, view=true);
  152.216 μs (13 allocations: 126.67 KiB)

julia> @btime dropmissing(df, view=true);
  1.432 ms (38 allocations: 1.63 MiB)

Main + this PR:

julia> @btime dropmissing(df, :a, view=true);
  17.093 μs (7 allocations: 122.34 KiB)

julia> @btime dropmissing(df, :b, view=true);
  162.656 μs (12 allocations: 126.59 KiB)

julia> @btime dropmissing(df, :c, view=true);
  168.988 μs (12 allocations: 126.59 KiB)

julia> @btime dropmissing(df, :d, view=true);
  1.890 ms (14 allocations: 6.99 MiB)

julia> @btime dropmissing(df, :e, view=true);
  1.100 ms (14 allocations: 3.94 MiB)

julia> @btime dropmissing(df, :f, view=true);
  166.613 μs (12 allocations: 126.59 KiB)

julia> @btime dropmissing(df, :g, view=true);
  188.653 μs (13 allocations: 126.67 KiB)
  
julia> @btime dropmissing(df, view=true);
  1.410 ms (38 allocations: 1.63 MiB)

end

Base.@propagate_inbounds function SubDataFrame(parent::DataFrame, rows::AbstractVector, cols)
Expand Down