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

Unexpected behavior (splatting/symbols) #145

Closed
bensetterholm opened this issue Feb 25, 2020 · 3 comments
Closed

Unexpected behavior (splatting/symbols) #145

bensetterholm opened this issue Feb 25, 2020 · 3 comments
Milestone

Comments

@bensetterholm
Copy link

Cross-post from discourse, at the request of @pdeffebach.

I was trying to find a way to programmatically generate a query by splatting a list of symbols into an @select. I found that the splat ... syntax, (or any objects containing whitespace or hyphens generated with the Symbol function for that matter), generated errors, though passing a variable with a list of symbols of any sort does seem to work.

Example:

using DataFrames

myDict = Dict()
myDict["aKey"] = 1:10
myDict["anotherKey"] = zeros(10)
myDict["a_nice_key"] = rand(Bool, 10)
myDict["a nasty key"] = fill("oof", 10)
df = DataFrame(myDict)

using DataFramesMeta, Lazy

colsOfInterest = [:anotherKey, :a_nice_key]

# Splatting errors, even though went you write out what the splat "evaluates to", it works.
#=
x = @> begin
    df
    @where(:aKey .< 4)
    @select(colsOfInterest...)
end
=#

# This also errors:
#=
x = @> begin
    df
    @where(:aKey .< 4)
    @select(:a_nice_key, Symbol("a nasty key"))
end
=#

# This surprisingly does not error:
pushfirst!(colsOfInterest, Symbol("a nasty key"))
x = @> begin
    df
    @where(:aKey .< 4)
    @select(colsOfInterest)
end
@bkamins
Copy link
Member

bkamins commented Feb 25, 2020

At some point, when this package is maintained, it should be decided what is best to do in these cases. For the time being here is what you should pass in two of cases that error for you to make them work:

# first error
x = @> begin
    df
    @where(:aKey .< 4)
    @select(colsOfInterest)
end

# second error
x = @> begin
    df
    @where(:aKey .< 4)
    @select(:a_nice_key, :var"a nasty key")
end

@pdeffebach
Copy link
Collaborator

Marking this as 1.X because working with multiple columns is important, but non-breaking.

@pdeffebach pdeffebach added this to the 1.X milestone Mar 7, 2021
@pdeffebach
Copy link
Collaborator

I'm going to close this, since the following works

julia> df = DataFrame(a = [1, 2], b = [3, 4]);
julia> @select(df, $(cols...))
2×2 DataFrame
 Row │ a      b     
     │ Int64  Int64 
─────┼──────────────
   1 │     1      3
   2 │     2      4

julia> @macroexpand(@select(df, $(cols...)))
quote
    #= /home/peterwd/Documents/Development/DataFramesMeta/src/macros.jl:1287 =#
    (DataFrames).select(df, cols...)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants