Skip to content

Commit

Permalink
Restrict getindex(::GroupedDataFrame, ::AbstractArray) to 1D arrays only
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Dec 9, 2019
1 parent 041af60 commit b97ec42
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/lib/indexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ The elements of a `GroupedDataFrame` are [`SubDataFrame`](@ref)s of its parent.
* `gd[key::GroupKey]` -> Get the group corresponding to the [`GroupKey`](@ref)
`key` (one of the elements of the vector returned by [`keys(::GroupedDataFrame)`](@ref)).
This should be nearly as fast as integer indexing.
* `gd[a::AbstractArray]` -> Select multiple groups and return them in a new
* `gd[a::AbstractVector]` -> Select multiple groups and return them in a new
`GroupedDataFrame` object. Groups may be selected by integer position using an
array of `Integer`s or `Bool`s, similar to a standard array. Alternatively the
array may contain keys of any of the types supported for dictionary-like
Expand Down
8 changes: 4 additions & 4 deletions src/groupeddataframe/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Base.getindex(gd::GroupedDataFrame, idx::Integer) =
view(gd.parent, gd.idx[gd.starts[idx]:gd.ends[idx]], :)

# Array of integers
function Base.getindex(gd::GroupedDataFrame, idxs::AbstractArray{T}) where {T<:Integer}
function Base.getindex(gd::GroupedDataFrame, idxs::AbstractVector{T}) where {T<:Integer}
new_starts = gd.starts[idxs]
new_ends = gd.ends[idxs]
if !allunique(new_starts)
Expand Down Expand Up @@ -443,12 +443,12 @@ end
Base.getindex(gd::GroupedDataFrame, key::GroupKeyTypes) = gd[_findgroup(gd, key)]

# Array indexing with dictionary-like keys
Base.getindex(gd::GroupedDataFrame, idxs::AbstractArray{T}) where {T<:GroupKeyTypes} = gd[[_findgroup(gd, k) for k in idxs]]
Base.getindex(gd::GroupedDataFrame, idxs::AbstractVector{T}) where {T<:GroupKeyTypes} = gd[[_findgroup(gd, k) for k in idxs]]

# InvertedIndex
function Base.getindex(gd::GroupedDataFrame, idx::InvertedIndex{T}) where T
selected = trues(length(gd))
if T <: AbstractArray
if T <: AbstractVector
selected[[_findgroup(gd, k) for k in idx.skip]] .= false
else
selected[_findgroup(gd, idx.skip)] = false
Expand All @@ -457,7 +457,7 @@ function Base.getindex(gd::GroupedDataFrame, idx::InvertedIndex{T}) where T
end

# InvertedIndex wrapping a boolean array
Base.getindex(gd::GroupedDataFrame, idx::InvertedIndex{T}) where {T<:AbstractArray{Bool}} = gd[.!idx.skip]
Base.getindex(gd::GroupedDataFrame, idx::InvertedIndex{T}) where {T<:AbstractVector{Bool}} = gd[.!idx.skip]

"""
get(gd::GroupedDataFrame, key, default)
Expand Down

0 comments on commit b97ec42

Please sign in to comment.