Skip to content

Commit

Permalink
add support for Not with multiple indices
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Mar 14, 2023
1 parent 0b3d620 commit 74547b3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# DataFrames.jl v1.6 Release Notes

## New functionalities

* `Not` allows for passing multiple postiional arguments that are
treated as if they were wrapped in `Cols`
([#3302](https://github.com/JuliaData/DataFrames.jl/pull/3302))

# DataFrames.jl v1.5 Release Notes

## New functionalities
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CategoricalArrays = "0.10.0"
Compat = "4.2"
DataAPI = "1.14.0"
InlineStrings = "1.3.0"
InvertedIndices = "1"
InvertedIndices = "1.3"
IteratorInterfaceExtensions = "0.1.1, 1"
Missings = "0.4.2, 1"
PooledArrays = "1.4.2"
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 @@ -33,6 +33,7 @@ The following values are a valid column index:
* a `Not` expression (see
[InvertedIndices.jl](https://github.com/JuliaData/InvertedIndices.jl));
`Not(idx)` selects all indices not in the passed `idx`;
when passed as column selector `Not(idx...)` is equivalent to `Not(Cols(idx...))`.
* a `Cols` expression (see
[DataAPI.jl](https://github.com/JuliaData/DataAPI.jl)); `Cols(idxs...)`
selects the union of the selections in `idxs`; in particular `Cols()`
Expand Down
2 changes: 2 additions & 0 deletions src/other/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ end
@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{InvertedIndices.NotMultiIndex}) =
setdiff(1:length(x), getindex(x, Cols(notidx.skip.indices...)))
@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)) : throw(ArgumentError("All(args...) is not supported: use Cols(args...) instead"))
Expand Down
8 changes: 8 additions & 0 deletions test/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ end
si7 = SubIndex(i, Not(1:2))
si8 = SubIndex(i, ["C", "D", "E"])
si9 = SubIndex(i, Not(Not(["C", "D", "E"])))
si10 = SubIndex(i, Not(1, 2))
si11 = SubIndex(i, Not(:A, :B))
si12 = SubIndex(i, Not(2, "A"))

@test copy(si1) == i
@test copy(si2) == Index([:C, :D, :E])
Expand All @@ -125,6 +128,9 @@ end
@test copy(si7) == Index([:C, :D, :E])
@test copy(si8) == Index([:C, :D, :E])
@test copy(si9) == Index([:C, :D, :E])
@test copy(si10) == Index([:C, :D, :E])
@test copy(si11) == Index([:C, :D, :E])
@test copy(si12) == Index([:C, :D, :E])

@test_throws ArgumentError SubIndex(i, 1)
@test_throws ArgumentError SubIndex(i, :A)
Expand Down Expand Up @@ -327,6 +333,8 @@ end
push!(i, :x131)
push!(i, :y13)
push!(i, :yy13)
@test i[Not(2, 4, 5)] == [1, 3]
@test i[Not(2, :y13, "yy13")] == [1, 3]
@test i[Not(Not(r"x1."))] == [2, 3]
@test isempty(i[Not(Not(r"xx"))])
@test i[Not(Not(r""))] == 1:5
Expand Down
9 changes: 9 additions & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ using Test, DataFrames, Unicode, Random
@test dfx[!, 1] === df[!, names(dfx)[1]]
end

@test df[!, Not(1, 2)] == DataFrame(c=7:9)
@test df[!, Not(:b, 1)] == DataFrame(c=7:9)
@test df[!, Not("c", :a)] == DataFrame(b=4:6)
@test df[!, Not(:b, "c", :a)] == DataFrame()
@test df[!, Not([1, 2], :b)] == DataFrame(c=7:9)
@test df[!, Not([:c, :a], :b)] == DataFrame()
@test df[!, Not([1, 2], 2)] == DataFrame(c=7:9)
@test df[!, Not([1, 2], [1, 2])] == DataFrame(c=7:9)

@test df[1, 1] == 1
@test df[1, 1:2] isa DataFrameRow
@test df[1, r"[ab]"] isa DataFrameRow
Expand Down

0 comments on commit 74547b3

Please sign in to comment.