What is the DataFramesMeta way to specify a column by its name in a variable?

What is the proper way to specify a column by a string variable containing the column name in a DataFramesMeta query? I tried Symbol(var) but got an error.

julia> df = DataFrame(num = 1:3)
3×1 DataFrame
│ Row │ num   │
│     │ Int64 │
├─────┼───────┤
│ 1   │ 1     │
│ 2   │ 2     │
│ 3   │ 3     │

Specifying the column using the colon notation works.

julia> @where(df, :num .== 2)
1×1 DataFrame
│ Row │ num   │
│     │ Int64 │
├─────┼───────┤
│ 1   │ 2     │

But using Symbol(var) in place of the colon notation does not work. Is this an expected behavior?

julia> colname = "num"
"num"

julia> @where(df, Symbol(colname) .== 2)
ERROR: ArgumentError: invalid row index of type Bool
Stacktrace:
 [1] DataFrameRow at /Users/nishina/.julia/packages/DataFrames/S3ZFo/src/dataframerow/dataframerow.jl:59 [inlined
]
 [2] getindex(::DataFrame, ::Bool, ::Colon) at /Users/nishina/.julia/packages/DataFrames/S3ZFo/src/dataframerow/d
ataframerow.jl:92
 [3] where(::DataFrame, ::var"#92#93") at /Users/nishina/.julia/packages/DataFramesMeta/c88dH/src/DataFramesMeta.
jl:186
 [4] top-level scope at REPL[50]:1

julia> Symbol(colname) == :num
true

As it is now, you need to use cols

colref = Symbol("num")
@where(df,cols(colref) .== 2)

Thank you. It worked.

julia> @where(df, cols(Symbol(colname)) .== 2)
1×1 DataFrame
│ Row │ num   │
│     │ Int64 │
├─────┼───────┤
│ 1   │ 2     │
1 Like

You could also combine @where and @with in which case using the symbol works.

crstnbr, sorry I don’t get it. Do you mean with @with I can rewrite the @where query without using cols?

I’m sorry, please ignore my comment, I’ve misread your question.

All right. No problem.