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 row-wise macros #267

Merged
merged 16 commits into from
Jul 25, 2021
1 change: 1 addition & 0 deletions src/DataFramesMeta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export @with,
@orderby,
@by, @combine,
@transform, @select, @transform!, @select!,
@rtransform,
bkamins marked this conversation as resolved.
Show resolved Hide resolved
@eachrow, @eachrow!,
@byrow,
@based_on, @where # deprecated
Expand Down
159 changes: 159 additions & 0 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,31 @@ macro subset(x, args...)
esc(subset_helper(x, args...))
end

function rsubset_helper(x, args...)
exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
throw(ArgumentError("Redundant @byrow calls"))
end

outer_flags[Symbol("@byrow")][] = true

t = (fun_to_vec(ex; no_dest=true, outer_flags=outer_flags) for ex in exprs)
quote
$subset($x, $(t...); skipmissing=true)
end
end


"""
@rsubset(d, i...)

Row-wise version of `@subset!`. See `? @subset!` for details.
"""
macro rsubset(x, args...)
esc(rsubset_helper(x, args...))
end


"""
@where(x, args...)

Expand All @@ -595,6 +620,30 @@ function subset!_helper(x, args...)
end
end

function rsubset!_helper(x, args...)
Copy link
Member

Choose a reason for hiding this comment

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

Would there be a way to avoid duplicating these helper functions, for example by passing subset or subset! to a more generic one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that't what DataFrameMacros does. I will do this in a future PR.

Copy link
Member

Choose a reason for hiding this comment

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

Why not do it now? :-p

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay I've simplified the @byrow check but it's hard to actually make everything in a coherent function

  1. The ugly t = (fun_to_vec(ex; no_dest=true, outer_flags=outer_flags) for ex in exprs) can't be gotten rid of yet because we still need to support some deprecated functionality in @combine.
  2. There are lots of exceptions, for example @subset requires a keyword argument, @orderby doesn't even call a DataFrames function. Writing a function for these cases would be just as easy as writing out the expression.

exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
throw(ArgumentError("Redundant @byrow calls"))
end

outer_flags[Symbol("@byrow")][] = true

t = (fun_to_vec(ex; no_dest=true, outer_flags=outer_flags) for ex in exprs)
quote
$subset!($x, $(t...); skipmissing=true)
end
end


"""
@rsubset!(d, i...)

Row-wise version of `@subset!`. See `? @subset!` for details.
"""
macro rsubset!(x, args...)
esc(rsubset!_helper(x, args...))
end

"""
@subset!(d, i...)

Expand Down Expand Up @@ -899,6 +948,30 @@ macro orderby(d, args...)
esc(orderby_helper(d, args...))
end

function rorderby_helper(x, args...)
exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
throw(ArgumentError("Redundant @byrow calls"))
end

outer_flags[Symbol("@byrow")][] = true


t = (fun_to_vec(ex; gensym_names = true, outer_flags = outer_flags) for ex in exprs)
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
quote
$DataFramesMeta.orderby($x, $(t...))
end
end

"""
rorderby(d, args...)

Row-wise version of `@orderby`. See `? @orderby` for details.
"""
macro rorderby(d, args...)
esc(rorderby_helper(d, args...))
end


##############################################################################
##
Expand Down Expand Up @@ -1017,6 +1090,27 @@ macro transform(x, args...)
esc(transform_helper(x, args...))
end

function rtransform_helper(x, args...)
exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
throw(ArgumentError("Redundant @byrow calls"))
end
outer_flags[Symbol("@byrow")][] = true

t = (fun_to_vec(ex; gensym_names = false, outer_flags = outer_flags) for ex in exprs)
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
quote
$DataFrames.transform($x, $(t...))
end
end

"""
@rtransform(x, args...)

Row-wise version of `@transform`, see `? @transform` for details.
"""
macro rtransform(x, args...)
esc(rtransform_helper(x, args...))
end

##############################################################################
##
Expand Down Expand Up @@ -1113,6 +1207,28 @@ macro transform!(x, args...)
esc(transform!_helper(x, args...))
end

function rtransform!_helper(x, args...)
exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
throw(ArgumentError("Redundant @byrow calls"))
end
outer_flags[Symbol("@byrow")][] = true

t = (fun_to_vec(ex; gensym_names = false, outer_flags = outer_flags) for ex in exprs)
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
quote
$DataFrames.transform!($x, $(t...))
end
end

"""
@rtransform!(x, args...)

Row-wise version of `@transform!`, see `? @transform!` for details.
"""
macro rtransform!(x, args...)
esc(rtransform_helper(x, args...))
end


##############################################################################
##
Expand Down Expand Up @@ -1227,6 +1343,28 @@ macro select(x, args...)
esc(select_helper(x, args...))
end

function rselect_helper(x, args...)
exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
throw(ArgumentError("Redundant @byrow calls"))
end
outer_flags[Symbol("@byrow")][] = true

t = (fun_to_vec(ex; gensym_names = false, outer_flags = outer_flags) for ex in exprs)
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
quote
$DataFrames.select($x, $(t...))
end
end

"""
@rselect(x, args...)

Row-wise version of `@select`, see `? @select` for details.
"""
macro rselect(x, args...)
esc(rselect_helper(x, args...))
end


##############################################################################
##
Expand Down Expand Up @@ -1336,6 +1474,27 @@ macro select!(x, args...)
esc(select!_helper(x, args...))
end

function rselect!_helper(x, args...)
exprs, outer_flags = create_args_vector(args...)
if outer_flags[Symbol("@byrow")][] == true
throw(ArgumentError("Redundant @byrow calls"))
end
outer_flags[Symbol("@byrow")][] = true

t = (fun_to_vec(ex; gensym_names = false, outer_flags = outer_flags) for ex in exprs)
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
quote
$DataFrames.select($x, $(t...))
end
end

"""
@rselect!(x, args...)

Row-wise version of `@select!`, see `? @select!` for details.
"""
macro rselect!(x, args...)
esc(rselect_helper(x, args...))
end

##############################################################################
##
Expand Down