Skip to content

Commit

Permalink
Fix error with ByRow on import (#366)
Browse files Browse the repository at this point in the history
* write errors

* special-case AsTable
  • Loading branch information
pdeffebach authored Nov 7, 2023
1 parent b15eb24 commit 1fd3c1a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ returns `nothing`.
get_column_expr(x) = nothing
function get_column_expr(e::Expr)
e.head == :$ && return e.args[1]
onearg(e, :AsTable) && return e
onearg(e, :AsTable) && return :($AsTable($(e.args[2])))
if onearg(e, :cols)
Base.depwarn("cols is deprecated use $DOLLAR to escape column names instead", :cols)
return e.args[2]
Expand Down Expand Up @@ -295,9 +295,9 @@ function get_source_fun(function_expr; exprflags = deepcopy(DEFAULT_FLAGS))

if exprflags[BYROW_SYM][]
if exprflags[PASSMISSING_SYM][]
fun = :(ByRow(Missings.passmissing($fun)))
fun = :($ByRow($(Missings.passmissing)($fun)))
else
fun = :(ByRow($fun))
fun = :($ByRow($fun))
end
end

Expand Down Expand Up @@ -350,7 +350,7 @@ function fun_to_vec(ex::Expr;
if final_flags[ASTABLE_SYM][]
src, fun = get_source_fun_astable(ex; exprflags = final_flags)

return :($src => $fun => AsTable)
return :($src => $fun => $AsTable)
end

if no_dest # subset and with
Expand Down
2 changes: 1 addition & 1 deletion src/parsing_astable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function get_source_fun_astable(ex; exprflags = deepcopy(DEFAULT_FLAGS))
# and if-so, making a named tuple with
# missing values
if exprflags[BYROW_SYM][]
fun = :(ByRow($fun))
fun = :($ByRow($fun))
end

return source, fun
Expand Down
34 changes: 34 additions & 0 deletions test/import.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module TestImport

using Test
import DataFrames
import DataFramesMeta

@testset "importing error" begin
df = DataFrames.DataFrame(a = [1, 2, 3])
t = DataFramesMeta.@rsubset df :a > 1
@test t == DataFrames.DataFrame(a = [2, 3])
t = DataFramesMeta.@transform df begin
@byrow :z = :a == 2
end
@test t == DataFrames.DataFrame(a = [1, 2, 3], z = [false, true, false])

t = DataFramesMeta.@rtransform df @astable begin
:b = 1
:c = 2
end
@test t == DataFrames.DataFrame(a = [1, 2, 3], b = [1, 1, 1], c = [2, 2, 2])

# AsTable on the RHS relies on the literal "AsTable" appearing
t = DataFramesMeta.@rtransform df :c = sum(AsTable([:a]))
@test t == DataFrames.DataFrame(a = [1, 2, 3], c = [1, 2, 3])

# And confusingly, if you use DataFrames.AsTable on the RHS, none of the
# special escaping happens.
# There is not a lot to do about this, unfortunately. I don't want
# to modify user-code.
@test_throws ArgumentError DataFramesMeta.@transform df :c = sum(DataFrames.AsTable([:a]))
end


end # module

0 comments on commit 1fd3c1a

Please sign in to comment.