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 not to indexing #1847

Merged
merged 23 commits into from
Jun 21, 2019
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
11 changes: 8 additions & 3 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ completecases(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not}) =

"""
dropmissing(df::AbstractDataFrame; disallowmissing::Bool=true)
dropmissing(df::AbstractDataFrame, cols; disallowmissing::Bool=true)
dropmissing(df::AbstractDataFrame, ::Colon; disallowmissing::Bool=true)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
dropmissing(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not}; disallowmissing::Bool=true)
dropmissing(df::AbstractDataFrame, cols::Union{Integer, Symbol}; disallowmissing::Bool=true)

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.
Expand Down Expand Up @@ -667,7 +669,9 @@ end

"""
dropmissing!(df::AbstractDataFrame; disallowmissing::Bool=true)
dropmissing!(df::AbstractDataFrame, cols; disallowmissing::Bool=true)
dropmissing!(df::AbstractDataFrame, ::Colon; disallowmissing::Bool=true)
dropmissing!(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not}; disallowmissing::Bool=true)
dropmissing!(df::AbstractDataFrame, cols::Union{Integer, Symbol}; disallowmissing::Bool=true)

Remove rows with missing values from data frame `df` and return it.
If `cols` is provided, only missing values in the corresponding columns are considered.
Expand Down Expand Up @@ -726,7 +730,8 @@ julia> dropmissing!(df3, [:x, :y])
```

"""
function dropmissing!(df::AbstractDataFrame, cols=:;
function dropmissing!(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:;
disallowmissing::Bool=true)
deleterows!(df, (!).(completecases(df, cols)))
disallowmissing && disallowmissing!(df, cols)
Expand Down
32 changes: 11 additions & 21 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ select!(df::DataFrame, c::Any) = select!(df, index(df)[c])
"""
select(df::AbstractDataFrame, inds, copycols::Bool=true)

If `df` is a data frame create a new data frame that contains columns from `df`
Create a new data frame that contains columns from `df`
specified by `inds` and return it.

Argument `inds` can be any index that is allowed for column indexing.
Expand Down Expand Up @@ -1053,19 +1053,14 @@ Base.hcat(df1::DataFrame, df2::AbstractDataFrame, dfn::AbstractDataFrame...;
##############################################################################
"""
allowmissing!(df::DataFrame)
allowmissing!(df::DataFrame, cols::Colon)
allowmissing!(df::DataFrame, cols::Union{Integer, Symbol})
allowmissing!(df::DataFrame, cols::Union{AbstractVector, Regex, Not})

Convert all columns of a `df` from element type `T` to
Convert columns `cols` of a `df` from element type `T` to
bkamins marked this conversation as resolved.
Show resolved Hide resolved
`Union{T, Missing}` to support missing values.

allowmissing!(df::DataFrame, col::Union{Integer, Symbol})

Convert a single column of a `df` from element type `T` to
`Union{T, Missing}` to support missing values.

allowmissing!(df::DataFrame, cols::AbstractVector})

Convert multiple columns of a `df` from element type `T` to
`Union{T, Missing}` to support missing values.
If `cols` is ommited all columns in the data frame are converted.
bkamins marked this conversation as resolved.
Show resolved Hide resolved
"""
function allowmissing! end

Expand Down Expand Up @@ -1097,19 +1092,14 @@ allowmissing!(df::DataFrame, cols::Colon=:) =

"""
disallowmissing!(df::DataFrame)

Convert all columns of a `df` from element type `Union{T, Missing}` to
`T` to drop support for missing values.

disallowmissing!(df::DataFrame, col::Union{Integer, Symbol})

Convert a single column of a `df` from element type `Union{T, Missing}` to
`T` to drop support for missing values.

disallowmissing!(df::DataFrame, cols::Colon)
disallowmissing!(df::DataFrame, cols::Union{Integer, Symbol})
disallowmissing!(df::DataFrame, cols::Union{AbstractVector, Regex, Not})

Convert multiple columns of a `df` from element type `Union{T, Missing}` to
Convert columns `cols` of a `df` from element type `Union{T, Missing}` to
bkamins marked this conversation as resolved.
Show resolved Hide resolved
`T` to drop support for missing values.

If `cols` is ommited all columns in the data frame are converted.
bkamins marked this conversation as resolved.
Show resolved Hide resolved
"""
function disallowmissing! end

Expand Down
10 changes: 5 additions & 5 deletions src/other/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ end

@inline function Base.getindex(x::AbstractIndex, idx::Integer)
if !(1 <= idx <= length(x))
throw(BoundsError("attempt to access $(length(x))-element AbstractIndex at index $idx"))
throw(BoundsError("attempt to access a data frame with $(length(x)) columns at index $idx"))
end
Int(idx)
end
Expand All @@ -150,10 +150,10 @@ end
isempty(idx) && return idx
minidx, maxidx = extrema(idx)
if minidx < 1
throw(BoundsError("attempt to access $(length(x))-element AbstractIndex at index $minidx"))
throw(BoundsError("attempt to access a data frame with $(length(x)) columns at index $minidx"))
end
if maxidx > length(x)
throw(BoundsError("attempt to access $(length(x))-element AbstractIndex at index $maxidx"))
throw(BoundsError("attempt to access a data frame with $(length(x)) columns at index $maxidx"))
end
allunique(idx) || throw(ArgumentError("Elements of $idx must be unique"))
idx
Expand All @@ -163,10 +163,10 @@ end
isempty(idx) && return idx
minidx, maxidx = extrema(idx)
if minidx < 1
throw(BoundsError("attempt to access $(length(x))-element AbstractIndex at index $minidx"))
throw(BoundsError("attempt to access a data frame with $(length(x)) columns at index $minidx"))
end
if maxidx > length(x)
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
throw(BoundsError("attempt to access $(length(x))-element AbstractIndex at index $maxidx"))
throw(BoundsError("attempt to access a data frame with $(length(x)) columns at index $maxidx"))
end
allunique(idx) || throw(ArgumentError("Elements of $idx must be unique"))
idx
Expand Down
19 changes: 18 additions & 1 deletion test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,21 @@ end
@test isempty(DataFrame(a=[], b=[]))
end

end # module
@testset "deletecols and deletecols!" begin
df = DataFrame(a=[1,2], b=[3.0, 4.0])
@test deletecols(df, :a) == DataFrame(b=[3.0, 4.0])
deletecols!(df, :a)
@test df == DataFrame(b=[3.0, 4.0])

df = DataFrame(a=[1,2], b=[3.0, 4.0])
@test deletecols(df, Not(2)) == DataFrame(b=[3.0, 4.0])
deletecols!(df, Not(2))
@test df == DataFrame(b=[3.0, 4.0])

df = DataFrame(a=[1,2], b=[3.0, 4.0])
@test deletecols(df, :a, copycols=false)[1] === df.b
@test deletecols(df, []) == df
@test deletecols(df, Not([])) == DataFrame()
end

end # module