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 an option to intersect arguments passed to Cols #3224

Merged
merged 6 commits into from
Dec 17, 2022
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Add `allunique` and allow transformations in `cols` argument of `describe`
and `nonunique` when working with `SubDataFrame`
([3232](https://github.com/JuliaData/DataFrames.jl/pull/3232))
* Add support for `operation` keyword argument in `Cols`
bkamins marked this conversation as resolved.
Show resolved Hide resolved
([3224](https://github.com/JuliaData/DataFrames.jl/pull/3224))

# DataFrames.jl v1.4.4 Patch Release Notes

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
[compat]
CategoricalArrays = "0.10.0"
Compat = "4.2"
DataAPI = "1.13.0"
DataAPI = "1.14.0"
InlineStrings = "1.3.0"
InvertedIndices = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
Expand Down
16 changes: 14 additions & 2 deletions src/other/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,20 @@ end
@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"))
@inline Base.getindex(x::AbstractIndex, idx::Cols) =
isempty(idx.cols) ? Int[] : union(getindex.(Ref(x), idx.cols)...)

@inline function Base.getindex(x::AbstractIndex, idx::Cols) =
bkamins marked this conversation as resolved.
Show resolved Hide resolved
isempty(idx.cols) && return Int[]
if idx.operation == :union
return union(getindex.(Ref(x), idx.cols)...)
elseif idx.operation == :intersect
return intersect(getindex.(Ref(x), idx.cols)...)
else
throw(ArgumentError("Unsupported operation :$(idx.operation). Either " *
":union or :intersect is required"))
end
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end
# the definition below is needed because `:` is Function

bkamins marked this conversation as resolved.
Show resolved Hide resolved
@inline Base.getindex(x::AbstractIndex, idx::Cols{Tuple{typeof(:)}}) = x[:]
@inline Base.getindex(x::AbstractIndex, idx::Cols{<:Tuple{Function}}) =
findall(idx.cols[1], names(x))
Expand Down
61 changes: 61 additions & 0 deletions test/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,67 @@ end
@test df[:, Cols(x -> x[end] == '3')] == DataFrame()
@test_throws ArgumentError df[:, Cols(x -> true, 1)]
@test_throws ArgumentError df[:, Cols(1, x -> true)]

@test ncol(select(df, Cols(operation=:intersect))) == 0
@test ncol(df[:, Cols(operation=:intersect)]) == 0

@test select(df, Cols(:, operation=:intersect)) == df[:, :]
@test df[:, Cols(, operation=:intersect:)] == df[:, :]

@test select(df, Cols(1, 2, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(1, :b, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(:a, 2, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(:a, :b, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(2, 1, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(:b, 1, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(2, :a, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(:b, :a, operation=:intersect)) == df[:, 2:1]

@test df[:, Cols(1, 2, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(1, :b, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:a, 2, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:a, :b, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(2, 1, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:b, 1, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(2, :a, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:b, :a, operation=:intersect)] == df[:, 2:1]

@test df[:, Cols(1, 1, 2, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:a, 1, :b, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:a, 2, :b, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(:a, :b, 2, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(2, 1, :a, operation=:intersect)] == df[:, 2:1]

@test select(df, Cols(1, "b", operation=:intersect)) == df[:, 2:1]
@test select(df, Cols("a", 2, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols("a", "b", operation=:intersect)) == df[:, 2:1]
@test select(df, Cols("b", 1, operation=:intersect)) == df[:, 2:1]
@test select(df, Cols(2, "a", operation=:intersect)) == df[:, 2:1]
@test select(df, Cols("b", "a", operation=:intersect)) == df[:, 2:1]

@test df[:, Cols(1, "b", operation=:intersect)] == df[:, 2:1]
@test df[:, Cols("a", 2, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols("a", "b", operation=:intersect)] == df[:, 2:1]
@test df[:, Cols("b", 1, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(2, "a", operation=:intersect)] == df[:, 2:1]
@test df[:, Cols("b", "a", operation=:intersect)] == df[:, 2:1]

@test df[:, Cols("a", 1, "b", operation=:intersect)] == df[:, 2:1]
@test df[:, Cols("a", 2, "b", operation=:intersect)] == df[:, 2:1]
@test df[:, Cols("a", "b", 2, operation=:intersect)] == df[:, 2:1]
@test df[:, Cols(2, 1, "a", operation=:intersect)] == df[:, 2:1]

df = DataFrame(a1=1, a2=2, b1=3, b2=4)
@test df[:, Cols(r"a", Not(r"1"), operation=:intersect)] == df[:, [:a2]]
@test df[:, Cols(Not(r"1"), r"a", operation=:intersect)] == df[:, [:a2]]
@test df[:, Cols(4:-1:1, 1:3, operation=:intersect)] == df[:, [3, 2]]
@test df[:, Cols(1:3, 4:-1:1, 1:3, operation=:intersect)] == df[:, [2, 3]]
@test df[:, Cols(1:3, 4:-1:1, 1:3, :b1, operation=:intersect)] == df[:, [:b1]]
@test df[:, Cols(x -> x[1] == 'a', operation=:intersect)] == df[:, [1, 2]]
@test df[:, Cols(x -> x[end] == '1', operation=:intersect)] == df[:, [1, 3]]
@test df[:, Cols(x -> x[end] == '3', operation=:intersect)] == DataFrame()
@test_throws ArgumentError df[:, Cols(x -> true, 1, operation=:intersect)]
@test_throws ArgumentError df[:, Cols(1, x -> true, operation=:intersect)]
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end

@testset "views" begin
Expand Down