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 Between and All #1914

Merged
merged 8 commits into from
Aug 12, 2019
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test = ["DataStructures", "DataValues", "Dates", "LaTeXStrings", "Random", "Test
julia = "1"
CategoricalArrays = ">= 0.5.4"
Compat = "2.0.0"
DataAPI = "1.0.1"
InvertedIndices = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
Missings = ">= 0.2.3"
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/indexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The rules for a valid type of index into a column are the following:
* a vector of `Bool` that has to be a subtype of `AbstractVector{Bool}`;
* a regular expression, which gets expanded to a vector of matching column names;
* a `Not` expression (see [InvertedIndices.jl](https://github.com/mbauman/InvertedIndices.jl));
* an `All` or `Between` expression (see [DataAPI.jl](https://github.com/JuliaData/DataAPI.jl));
* a colon literal `:`.

The rules for a valid type of index into a row are the following:
Expand Down
7 changes: 5 additions & 2 deletions src/DataFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ using Base.Sort, Base.Order, Base.Iterators
##
##############################################################################

import DataAPI.describe
import DataAPI.All,
DataAPI.Between,
DataAPI.describe

export AbstractDataFrame,
All,
Between,
DataFrame,
DataFrame!,
DataFrameRow,
GroupedDataFrame,
SubDataFrame,

allowmissing!,
aggregate,
by,
Expand Down
24 changes: 12 additions & 12 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ end

"""
completecases(df::AbstractDataFrame, cols::Colon=:)
completecases(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not})
completecases(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not, Between, All})
completecases(df::AbstractDataFrame, cols::Union{Integer, Symbol})

Return a Boolean vector with `true` entries indicating rows without missing values
Expand Down Expand Up @@ -594,12 +594,12 @@ function completecases(df::AbstractDataFrame, col::ColumnIndex)
res
end

completecases(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not}) =
completecases(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not, Between, All}) =
completecases(df[!, cols])

"""
dropmissing(df::AbstractDataFrame, cols::Colon=:; disallowmissing::Bool=true)
dropmissing(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not};
dropmissing(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not, Between, All};
disallowmissing::Bool=true)
dropmissing(df::AbstractDataFrame, cols::Union{Integer, Symbol};
disallowmissing::Bool=true)
Expand Down Expand Up @@ -664,7 +664,7 @@ julia> dropmissing(df, [:x, :y])

"""
function dropmissing(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:;
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon}=:;
disallowmissing::Bool=true)
newdf = df[completecases(df, cols), :]
disallowmissing && disallowmissing!(newdf, cols)
Expand All @@ -673,7 +673,7 @@ end

"""
dropmissing!(df::AbstractDataFrame, cols::Colon=:; disallowmissing::Bool=true)
dropmissing!(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not};
dropmissing!(df::AbstractDataFrame, cols::Union{AbstractVector, Regex, Not, Between, All};
disallowmissing::Bool=true)
dropmissing!(df::AbstractDataFrame, cols::Union{Integer, Symbol};
disallowmissing::Bool=true)
Expand Down Expand Up @@ -736,7 +736,7 @@ julia> dropmissing!(df3, [:x, :y])

"""
function dropmissing!(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:;
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon}=:;
disallowmissing::Bool=true)
deleterows!(df, (!).(completecases(df, cols)))
disallowmissing && disallowmissing!(df, cols)
Expand Down Expand Up @@ -1286,7 +1286,7 @@ julia> ncol(df)

"""
disallowmissing(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:)
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon}=:)

Return a copy of data frame `df` with columns `cols` converted
from element type `Union{T, Missing}` to `T` to drop support for missing values.
Expand Down Expand Up @@ -1314,7 +1314,7 @@ julia> disallowmissing(df)
```
"""
function Missings.disallowmissing(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:)
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon}=:)
idxcols = Set(index(df)[cols])
newcols = AbstractVector[]
for i in axes(df, 2)
Expand All @@ -1331,7 +1331,7 @@ end

"""
allowmissing(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:)
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon}=:)

Return a copy of data frame `df` with columns `cols` converted
to element type `Union{T, Missing}` from `T` to allow support for missing values.
Expand Down Expand Up @@ -1359,7 +1359,7 @@ julia> allowmissing(df)
```
"""
function Missings.allowmissing(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon}=:)
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon}=:)
idxcols = Set(index(df)[cols])
newcols = AbstractVector[]
for i in axes(df, 2)
Expand All @@ -1377,7 +1377,7 @@ end
"""
categorical(df::AbstractDataFrame; compress::Bool=false)
categorical(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon};
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon};
compress::Bool=false)

Return a copy of data frame `df` with columns `cols` converted to `CategoricalVector`.
Expand Down Expand Up @@ -1419,7 +1419,7 @@ julia> categorical(df, :)

"""
function CategoricalArrays.categorical(df::AbstractDataFrame,
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Colon};
cols::Union{ColumnIndex, AbstractVector, Regex, Not, Between, All, Colon};
compress::Bool=false)
idxcols = Set(index(df)[cols])
newcols = AbstractVector[]
Expand Down
24 changes: 12 additions & 12 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ end

# df[MultiRowIndex, MultiColumnIndex] => DataFrame
@inline function Base.getindex(df::DataFrame, row_inds::AbstractVector{T},
col_inds::Union{AbstractVector, Regex, Not}) where T
col_inds::Union{AbstractVector, Regex, Not, Between, All}) where T
@boundscheck if !checkindex(Bool, axes(df, 1), row_inds)
throw(BoundsError("attempt to access a data frame with $(nrow(df)) " *
"rows at index $row_inds"))
Expand All @@ -394,15 +394,15 @@ end
end

@inline Base.getindex(df::DataFrame, row_inds::Not,
col_inds::Union{AbstractVector, Regex, Not, Colon}) =
col_inds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
df[axes(df, 1)[row_inds], col_inds]

# df[:, MultiColumnIndex] => DataFrame
Base.getindex(df::DataFrame, row_ind::Colon, col_inds::Union{AbstractVector, Regex, Not, Colon}) =
Base.getindex(df::DataFrame, row_ind::Colon, col_inds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
select(df, col_inds, copycols=true)

# df[!, MultiColumnIndex] => DataFrame
Base.getindex(df::DataFrame, row_ind::typeof(!), col_inds::Union{AbstractVector, Regex, Not, Colon}) =
Base.getindex(df::DataFrame, row_ind::typeof(!), col_inds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
select(df, col_inds, copycols=false)

##############################################################################
Expand Down Expand Up @@ -518,7 +518,7 @@ end
function Base.setindex!(df::DataFrame,
new_df::AbstractDataFrame,
row_inds::Union{AbstractVector, Not}, # add Colon after deprecation
col_inds::Union{AbstractVector, Regex, Not}) # add Colon after deprecation
col_inds::Union{AbstractVector, Regex, Not, Between, All}) # add Colon after deprecation
idxs = index(df)[col_inds]
if view(_names(df), idxs) != _names(new_df)
Base.depwarn("in the future column names in source and target will have to match", :setindex!)
Expand All @@ -533,7 +533,7 @@ end
function Base.setindex!(df::DataFrame,
mx::AbstractMatrix,
row_inds::Union{AbstractVector, Not}, # add Colon after deprecation
col_inds::Union{AbstractVector, Regex, Not}) # add Colon after deprecation
col_inds::Union{AbstractVector, Regex, Not, Between, All}) # add Colon after deprecation
idxs = index(df)[col_inds]
if size(mx, 2) != length(idxs)
throw(DimensionMismatch("number of selected columns ($(length(idxs))) and number of columns in" *
Expand Down Expand Up @@ -886,7 +886,7 @@ Base.hcat(df1::DataFrame, df2::AbstractDataFrame, dfn::AbstractDataFrame...;
"""
allowmissing!(df::DataFrame, cols::Colon=:)
allowmissing!(df::DataFrame, cols::Union{Integer, Symbol})
allowmissing!(df::DataFrame, cols::Union{AbstractVector, Regex, Not})
allowmissing!(df::DataFrame, cols::Union{AbstractVector, Regex, Not, Between, All})

Convert columns `cols` of data frame `df` from element type `T` to
`Union{T, Missing}` to support missing values.
Expand Down Expand Up @@ -915,7 +915,7 @@ function allowmissing!(df::DataFrame, cols::AbstractVector{Bool})
df
end

allowmissing!(df::DataFrame, cols::Union{Regex, Not}) =
allowmissing!(df::DataFrame, cols::Union{Regex, Not, Between, All}) =
allowmissing!(df, index(df)[cols])

allowmissing!(df::DataFrame, cols::Colon=:) =
Expand All @@ -924,7 +924,7 @@ allowmissing!(df::DataFrame, cols::Colon=:) =
"""
disallowmissing!(df::DataFrame, cols::Colon=:)
disallowmissing!(df::DataFrame, cols::Union{Integer, Symbol})
disallowmissing!(df::DataFrame, cols::Union{AbstractVector, Regex, Not})
disallowmissing!(df::DataFrame, cols::Union{AbstractVector, Regex, Not, Between, All})

Convert columns `cols` of data frame `df` from element type `Union{T, Missing}` to
`T` to drop support for missing values.
Expand Down Expand Up @@ -953,7 +953,7 @@ function disallowmissing!(df::DataFrame, cols::AbstractVector{Bool})
df
end

disallowmissing!(df::DataFrame, cols::Union{Regex, Not}) =
disallowmissing!(df::DataFrame, cols::Union{Regex, Not, Between, All}) =
disallowmissing!(df, index(df)[cols])

disallowmissing!(df::DataFrame, cols::Colon=:) =
Expand All @@ -970,7 +970,7 @@ disallowmissing!(df::DataFrame, cols::Colon=:) =
compress::Bool=false)
categorical!(df::DataFrame, cnames::Vector{<:Union{Integer, Symbol}};
compress::Bool=false)
categorical!(df::DataFrame, cnames::Union{Regex, Not};
categorical!(df::DataFrame, cnames::Union{Regex, Not, Between, All};
compress::Bool=false)
categorical!(df::DataFrame; compress::Bool=false)

Expand Down Expand Up @@ -1047,7 +1047,7 @@ function categorical!(df::DataFrame, cnames::AbstractVector{<:ColumnIndex};
df
end

categorical!(df::DataFrame, cnames::Union{Regex, Not, Colon}; compress::Bool=false) =
categorical!(df::DataFrame, cnames::Union{Regex, Not, Between, All, Colon}; compress::Bool=false) =
categorical!(df, index(df)[cnames], compress=compress)

function categorical!(df::DataFrame; compress::Bool=false)
Expand Down
8 changes: 4 additions & 4 deletions src/dataframerow/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ Base.parent(r::DataFrameRow) = getfield(r, :df)
Base.parentindices(r::DataFrameRow) = (row(r), parentcols(index(r)))

Base.@propagate_inbounds Base.view(adf::AbstractDataFrame, rowind::Integer,
colinds::Union{Colon, AbstractVector, Regex, Not}) =
colinds::Union{Colon, AbstractVector, Regex, Not, Between, All}) =
DataFrameRow(adf, rowind, colinds)

Base.@propagate_inbounds Base.getindex(df::AbstractDataFrame, rowind::Integer,
colinds::Union{AbstractVector, Regex, Not}) =
colinds::Union{AbstractVector, Regex, Not, Between, All}) =
DataFrameRow(df, rowind, colinds)
Base.@propagate_inbounds Base.getindex(df::AbstractDataFrame, rowind::Integer, ::Colon) =
DataFrameRow(df, rowind, :)
Base.@propagate_inbounds Base.getindex(r::DataFrameRow, idx::ColumnIndex) =
parent(r)[row(r), parentcols(index(r), idx)]
Base.@propagate_inbounds Base.getindex(r::DataFrameRow, idxs::Union{AbstractVector, Regex, Not}) =
Base.@propagate_inbounds Base.getindex(r::DataFrameRow, idxs::Union{AbstractVector, Regex, Not, Between, All}) =
DataFrameRow(parent(r), row(r), parentcols(index(r), idxs))
Base.@propagate_inbounds Base.getindex(r::DataFrameRow, ::Colon) = r

Expand Down Expand Up @@ -132,7 +132,7 @@ Base.propertynames(r::DataFrameRow, private::Bool=false) = names(r)

Base.view(r::DataFrameRow, col::ColumnIndex) =
view(parent(r)[!, parentcols(index(r), col)], row(r))
Base.view(r::DataFrameRow, cols::Union{AbstractVector, Regex, Not}) =
Base.view(r::DataFrameRow, cols::Union{AbstractVector, Regex, Not, Between, All}) =
DataFrameRow(parent(r), row(r), parentcols(index(r), cols))
Base.view(r::DataFrameRow, ::Colon) = r

Expand Down
9 changes: 7 additions & 2 deletions src/other/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,14 @@ end
idx
end

@inline Base.getindex(x::AbstractIndex, idx::AbstractRange{<:Integer}) = getindex(x, collect(Int, idx))
@inline Base.getindex(x::AbstractIndex, idx::AbstractRange{<:Integer}) =
getindex(x, collect(Int, idx))
@inline Base.getindex(x::AbstractIndex, ::Colon) = Base.OneTo(length(x))
@inline Base.getindex(x::AbstractIndex, notidx::Not) = setdiff(1:length(x), getindex(x, notidx.skip))
@inline Base.getindex(x::AbstractIndex, notidx::Not) =
setdiff(1:length(x), getindex(x, notidx.skip))
@inline Base.getindex(x::AbstractIndex, idx::Between) = x[idx.first]:x[idx.last]
@inline Base.getindex(x::AbstractIndex, idx::All) =
isempty(idx.cols) ? (1:length(x)) : union(getindex.(Ref(x), idx.cols)...)

@inline function Base.getindex(x::AbstractIndex, idx::AbstractVector{<:Integer})
if any(v -> v isa Bool, idx)
Expand Down
12 changes: 6 additions & 6 deletions src/subdataframe/subdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ Base.@propagate_inbounds Base.view(adf::AbstractDataFrame, ::typeof(!), colind::
@inline Base.view(adf::AbstractDataFrame, rowinds, colind::Bool) =
throw(ArgumentError("invalid column index $colind of type `Bool`"))
Base.@propagate_inbounds Base.view(adf::AbstractDataFrame, rowinds,
colinds::Union{AbstractVector, Regex, Not, Colon}) =
colinds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
SubDataFrame(adf, rowinds, colinds)
Base.@propagate_inbounds Base.view(adf::AbstractDataFrame, rowinds::typeof(!),
colinds::Union{AbstractVector, Regex, Not, Colon}) =
colinds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
SubDataFrame(adf, :, colinds)
Base.@propagate_inbounds Base.view(adf::AbstractDataFrame, rowinds::Not,
colinds::Union{AbstractVector, Regex, Not, Colon}) =
colinds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
SubDataFrame(adf, axes(adf, 1)[rowinds], colinds)

##############################################################################
Expand Down Expand Up @@ -128,13 +128,13 @@ Base.@propagate_inbounds Base.getindex(sdf::SubDataFrame, ::typeof(!), colind::C
view(parent(sdf), rows(sdf), parentcols(index(sdf), colind))

Base.@propagate_inbounds Base.getindex(sdf::SubDataFrame, rowinds::Union{AbstractVector, Not},
colinds::Union{AbstractVector, Regex, Not, Colon}) =
colinds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
parent(sdf)[rows(sdf)[rowinds], parentcols(index(sdf), colinds)]
Base.@propagate_inbounds Base.getindex(sdf::SubDataFrame, ::Colon,
colinds::Union{AbstractVector, Regex, Not, Colon}) =
colinds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
parent(sdf)[rows(sdf), parentcols(index(sdf), colinds)]
Base.@propagate_inbounds Base.getindex(df::SubDataFrame, row_ind::typeof(!),
col_inds::Union{AbstractVector, Regex, Not, Colon}) =
col_inds::Union{AbstractVector, Regex, Not, Between, All, Colon}) =
select(df, col_inds, copycols=false)


Expand Down
Loading