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

add view to filter, sort, dropmissing, and unique #2386

Merged
merged 19 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
88 changes: 56 additions & 32 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -747,15 +747,19 @@ completecases(df::AbstractDataFrame, cols::MultiColumnIndex) =
completecases(df[!, cols])

"""
dropmissing(df::AbstractDataFrame, cols=:; disallowmissing::Bool=true)
dropmissing(df::AbstractDataFrame, cols=:; view::Bool=false, disallowmissing::Bool=!view)

Return a copy of data frame `df` excluding rows with missing values.

If `cols` is provided, only missing values in the corresponding columns are considered.
`cols` can be any column selector ($COLUMNINDEX_STR; $MULTICOLUMNINDEX_STR).

If `disallowmissing` is `true` (the default) then columns specified in `cols` will
be converted so as not to allow for missing values using [`disallowmissing!`](@ref).
If `disallowmissing` is `true` (the default when `view` is `false`)
then columns specified in `cols` will be converted so as not to allow for missing
values using [`disallowmissing!`](@ref).

If `view=true` then a view into `df` is returned instead. In this case
`disallowmissing` must be `false`.

See also: [`completecases`](@ref) and [`dropmissing!`](@ref).

Expand Down Expand Up @@ -811,10 +815,14 @@ julia> dropmissing(df, [:x, :y])
"""
function dropmissing(df::AbstractDataFrame,
cols::Union{ColumnIndex, MultiColumnIndex}=:;
bkamins marked this conversation as resolved.
Show resolved Hide resolved
disallowmissing::Bool=true)
newdf = df[completecases(df, cols), :]
view::Bool=false, disallowmissing::Bool=!view)
view && disallowmissing && throw(ArgumentError("it is not possible to disallow " *
"missing when view=true"))
bkamins marked this conversation as resolved.
Show resolved Hide resolved
rowidxs = completecases(df, cols)
view && return Base.view(df, completecases(df, cols), :)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
newdf = df[rowidxs, :]
disallowmissing && disallowmissing!(newdf, cols)
newdf
return newdf
end

"""
Expand Down Expand Up @@ -887,8 +895,8 @@ function dropmissing!(df::AbstractDataFrame,
end

"""
filter(fun, df::AbstractDataFrame)
filter(cols => fun, df::AbstractDataFrame)
filter(fun, df::AbstractDataFrame; view::Bool=false)
filter(cols => fun, df::AbstractDataFrame; view::Bool=false)

Return a copy of data frame `df` containing only rows for which `fun`
returns `true`.
Expand All @@ -902,6 +910,8 @@ corresponding columns as separate positional arguments, unless `cols` is an
column duplicates are allowed if a vector of `Symbol`s, strings, or integers is
passed.

If `view=true` then a view into `df` is returned instead.

Passing `cols` leads to a more efficient execution of the operation for large data frames.

See also: [`filter!`](@ref)
Expand Down Expand Up @@ -953,38 +963,45 @@ julia> filter(AsTable(:) => nt -> nt.x == 1 || nt.y == "b", df)
│ 3 │ 1 │ b │
```
"""
Base.filter(f, df::AbstractDataFrame) = _filter_helper(df, f, eachrow(df))
Base.filter((col, f)::Pair{<:ColumnIndex}, df::AbstractDataFrame) =
_filter_helper(df, f, df[!, col])
Base.filter((cols, f)::Pair{<:AbstractVector{Symbol}}, df::AbstractDataFrame) =
filter([index(df)[col] for col in cols] => f, df)
Base.filter((cols, f)::Pair{<:AbstractVector{<:AbstractString}}, df::AbstractDataFrame) =
filter([index(df)[col] for col in cols] => f, df)
Base.filter((cols, f)::Pair, df::AbstractDataFrame) =
filter(index(df)[cols] => f, df)

function Base.filter((cols, f)::Pair{<:AbstractVector{Int}}, df::AbstractDataFrame)
Base.filter(f, df::AbstractDataFrame; view::Bool=false) =
_filter_helper(df, f, eachrow(df), view=view)
Base.filter((col, f)::Pair{<:ColumnIndex}, df::AbstractDataFrame; view::Bool=false) =
_filter_helper(df, f, df[!, col], view=view)
Base.filter((cols, f)::Pair{<:AbstractVector{Symbol}}, df::AbstractDataFrame;
view::Bool=false) =
filter([index(df)[col] for col in cols] => f, df, view=view)
Base.filter((cols, f)::Pair{<:AbstractVector{<:AbstractString}}, df::AbstractDataFrame;
view::Bool=false) =
filter([index(df)[col] for col in cols] => f, df, view=view)
Base.filter((cols, f)::Pair, df::AbstractDataFrame; view::Bool=false) =
filter(index(df)[cols] => f, df, view=view)

function Base.filter((cols, f)::Pair{<:AbstractVector{Int}}, df::AbstractDataFrame;
view::Bool=false)
cdf = _columns(df)
return _filter_helper(df, f, (cdf[i] for i in cols)...)
return _filter_helper(df, f, (cdf[i] for i in cols)...; view=view)
end

function _filter_helper(df::AbstractDataFrame, f, cols...)
function _filter_helper(df::AbstractDataFrame, f, cols...; view::Bool)
if length(cols) == 0
throw(ArgumentError("At least one column must be passed to filter on"))
end
return df[((x...) -> f(x...)::Bool).(cols...), :]
rowidxs = ((x...) -> f(x...)::Bool).(cols...)
return view ? Base.view(df, rowidxs, :) : df[rowidxs, :]
end

function Base.filter((cols, f)::Pair{<:AsTable}, df::AbstractDataFrame)
function Base.filter((cols, f)::Pair{<:AsTable}, df::AbstractDataFrame; view::Bool=false)
df_tmp = select(df, cols.cols, copycols=false)
if ncol(df_tmp) == 0
throw(ArgumentError("At least one column must be passed to filter on"))
end
return _filter_helper_astable(df, Tables.namedtupleiterator(df_tmp), f)
return _filter_helper_astable(df, Tables.namedtupleiterator(df_tmp), f, view=view)
end

_filter_helper_astable(df::AbstractDataFrame, nti::Tables.NamedTupleIterator, f) =
df[(x -> f(x)::Bool).(nti), :]
function _filter_helper_astable(df::AbstractDataFrame, nti::Tables.NamedTupleIterator, f; view::Bool)
rowidxs = (x -> f(x)::Bool).(nti)
return view ? Base.view(df, rowidxs, :) : df[rowidxs, :]
end

"""
filter!(fun, df::AbstractDataFrame)
Expand Down Expand Up @@ -1175,13 +1192,19 @@ Base.unique!(df::AbstractDataFrame, cols) =
delete!(df, findall(nonunique(df, cols)))

# Unique rows of an AbstractDataFrame.
Base.unique(df::AbstractDataFrame) = df[(!).(nonunique(df)), :]
Base.unique(df::AbstractDataFrame, cols) =
df[(!).(nonunique(df, cols)), :]
function Base.unique(df::AbstractDataFrame; view::Bool=false)
rowidxs = (!).(nonunique(df))
return view ? view(df, rowidxs, :) : df[rowidxs, :]
end

function Base.unique(df::AbstractDataFrame, cols; view::Bool=false)
rowidxs = (!).(nonunique(df, cols))
return view ? view(df, rowidxs, :) : df[rowidxs, :]
end

"""
unique(df::AbstractDataFrame)
unique(df::AbstractDataFrame, cols)
unique(df::AbstractDataFrame; view::Bool=false)
unique(df::AbstractDataFrame, cols; view::Bool=false)
unique!(df::AbstractDataFrame)
unique!(df::AbstractDataFrame, cols)

Expand All @@ -1190,7 +1213,8 @@ When `cols` is specified, the returned `DataFrame` contains complete rows,
retaining in each case the first instance for which `df[cols]` is unique.
`cols` can be any column selector ($COLUMNINDEX_STR; $MULTICOLUMNINDEX_STR).

When `unique` is called a new data frame is returned; `unique!` updates `df` in-place.
When `unique` is called a new data frame is returned unless `view=true` in which
case a view into `df` is returned instead; `unique!` updates `df` in-place.
bkamins marked this conversation as resolved.
Show resolved Hide resolved

See also [`nonunique`](@ref).

Expand Down
12 changes: 8 additions & 4 deletions src/abstractdataframe/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,10 @@ end
"""
sort(df::AbstractDataFrame, cols;
alg::Union{Algorithm, Nothing}=nothing, lt=isless, by=identity,
rev::Bool=false, order::Ordering=Forward)
rev::Bool=false, order::Ordering=Forward, view::Bool=false)

Return a copy of data frame `df` sorted by column(s) `cols`.
Return a copy of data frame `df` sorted by column(s) `cols` unless
`view=true` in which case a view into `df` is returned instead.
bkamins marked this conversation as resolved.
Show resolved Hide resolved

`cols` can be any column selector ($COLUMNINDEX_STR; $MULTICOLUMNINDEX_STR).

Expand Down Expand Up @@ -487,8 +488,11 @@ julia> sortperm(df, (:x, :y), rev=true)
"""
sortperm(::AbstractDataFrame, ::Any)

Base.sort(df::AbstractDataFrame, a::Algorithm, o::Ordering) =
df[sortperm(df, a, o),:]
function Base.sort(df::AbstractDataFrame, a::Algorithm, o::Ordering; view::Bool=false)
rowidxs = sortperm(df, a, o)
return view ? Base.view(df, rowidxs, :) : df[rowidxs, :]
end

Base.sortperm(df::AbstractDataFrame, a::Algorithm, o::Union{Perm,DFPerm}) =
sort!([1:size(df, 1);], a, o)
Base.sortperm(df::AbstractDataFrame, a::Algorithm, o::Ordering) =
Expand Down