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 @subset #263

Merged
merged 15 commits into from
Jun 29, 2021
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in C#.
In addition, DataFramesMeta provides

* `@orderby`, for sorting data frames
* `@subset` and `@subset!`, for keeping rows of a DataFrame matching a given condition
* `@subset` and `@subset!`, for keeping rows of a data frame matching a given condition
* `@by`, for grouping and combining a data frame in a single step
* `@with`, for working with the columns of a data frame with high performance and
convenient syntax
Expand Down
4 changes: 2 additions & 2 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ and

!!! note
`@subset` treats `missing` values as `false` when filtering rows.
Unlike `DataFrames.subset` and other boolean operations with
Unlike `DataFrames.subset` and other Boolean operations with
`missing`, `@subset` will *not* error on missing values, and
will only keep `true` values.

Expand Down Expand Up @@ -631,7 +631,7 @@ and

!!! note
`@subset!` treats `missing` values as `false` when filtering rows.
Unlike `DataFrames.subset!` and other boolean operations with
Unlike `DataFrames.subset!` and other Boolean operations with
`missing`, `@subset!` will *not* error on missing values, and
will only keep `true` values.

Expand Down
20 changes: 12 additions & 8 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ end
# We need wrap_byrow as a keyword argument here in case someone
# uses `@transform df @byrow begin ... end`, which we
# deal with outside of this function.
function fun_to_vec(ex::Expr; gensym_names::Bool=false, outer_flags=nothing, no_dest::Bool=false)
function fun_to_vec(ex::Expr;
gensym_names::Bool=false,
outer_flags::Union{NamedTuple, Nothing}=nothing,
no_dest::Bool=false)
# classify the type of expression
# :x # handled via dispatch
# cols(:x) # handled as though above
Expand Down Expand Up @@ -333,19 +336,20 @@ function replace_dotted!(e, membernames)
Expr(:., x_new, y_new)
end

"""
create_args_vector(args...) -> vec, wrap_byrow

Given multiple arguments which can be any type
of expression-like object (`Expr`, `QuoteNode`, etc.),
puts them into a single array, removing line numbers.
"""
function create_args_vector(args...)
create_args_vector(Expr(:block, args...))
end

"""
create_args_vector(arg) -> vec, outer_flags

Given an expression return a vector of operations
and a `NamedTuple` of the macro-flags that appear
in the expression.

If a `:block` expression, returns the `args` of
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
the block as an array. If a simple expression,
wrap the expression in a one-element vector.
"""
function create_args_vector(arg)
arg, outer_flags = extract_macro_flags(MacroTools.unblock(arg))
Expand Down
6 changes: 3 additions & 3 deletions test/dataframes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -700,17 +700,17 @@ end

@test d.y == [true]

d = @where(df, @linenums_macro begin end)
d = @subset(df, @linenums_macro begin end)

@test nrow(d) == 1

d = @where df begin
d = @subset df begin
@byrow @linenums_macro_byrow begin end
end

@test nrow(d) == 1

d = @where df @byrow begin
d = @subset df @byrow begin
@linenums_macro_byrow begin end
end

Expand Down
15 changes: 15 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,20 @@ end
@test d ≅ @where(df, :A .> 1, :B .> 1)
end

@testset "@where with a grouped data frame" begin
df = DataFrame(
g = [1, 1, 1, 2, 2],
i = 1:5,
t = ["a", "b", "c", "c", "e"],
y = [:v, :w, :x, :y, :z],
c = [:g, :quote, :body, :transform, missing]
)

gd = groupby(df, :g)

@test @where(gd, :i .== first(:i)) ≅ df[[1, 4], :]
@test @where(gd, cols(:i) .> mean(cols(:i)), :t .== "c") ≅ df[[3], :]
@test @where(gd, :c .== :g) ≅ df[[], :]
end

end # module
15 changes: 0 additions & 15 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,19 +357,4 @@ end
@test @select(g, :a, @byrow t = :a ^ 2).t ≅ d.a .^ 2
end

@testset "@where with a grouped data frame" begin
df = DataFrame(
g = [1, 1, 1, 2, 2],
i = 1:5,
t = ["a", "b", "c", "c", "e"],
y = [:v, :w, :x, :y, :z],
c = [:g, :quote, :body, :transform, missing]
)

gd = groupby(df, :g)

@test @where(gd, :i .== first(:i)) ≅ df[[1, 4], :]
@test @where(gd, cols(:i) .> mean(cols(:i)), :t .== "c") ≅ df[[3], :]
@test @where(gd, :c .== :g) ≅ df[[], :]
end
end # module
22 changes: 20 additions & 2 deletions test/subset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ end

x = [2, 1, 0, 0]

@test @subset!(copy(df), :A .> 1) == df[(df.A .> 1) .=== true,:]
df2 = copy(df)
@test @subset!(df2, :A .> 1) === df2
@test df2 == df[(df.A .> 1) .=== true,:]
@test @subset!(copy(df), :B .> 1) == df[df.B .> 1,:]
@test @subset!(copy(df), :A .> x) == df[(df.A .> x) .=== true,:]
@test @subset!(copy(df), :B .> x) ≅ df[df.B .> x,:]
Expand Down Expand Up @@ -140,4 +142,20 @@ end
@test d ≅ @subset!(copy(df), :A .> 1, :B .> 1)
end

end # module
@testset "@subset with a grouped data frame" begin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test @subset! with GroupedDataFrame?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

df = DataFrame(
g = [1, 1, 1, 2, 2],
i = 1:5,
t = ["a", "b", "c", "c", "e"],
y = [:v, :w, :x, :y, :z],
c = [:g, :quote, :body, :transform, missing]
)

gd = groupby(df, :g)

@test @subset(gd, :i .== first(:i)) ≅ df[[1, 4], :]
@test @subset(gd, cols(:i) .> mean(cols(:i)), :t .== "c") ≅ df[[3], :]
@test @subset(gd, :c .== :g) ≅ df[[], :]
end

end # module