In this example, I’m trying to define functions for accessing data frame columns. However, the interpolation within the getindex
call ended up with a type rather than the original symbol… Why is that? What can I do to fix it?
julia> module Foo
using DataFrames
for f in (:x, :y)
@eval $(f)(df::AbstractDataFrame) = df[:, $f]
end
end
Main.Foo
julia> df = DataFrame(x = rand(3), y = rand(3))
3×2 DataFrame
│ Row │ x │ y │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 0.206567 │ 0.238623 │
│ 2 │ 0.467982 │ 0.219791 │
│ 3 │ 0.219056 │ 0.158868 │
julia> Foo.x(df)
ERROR: MethodError: no method matching getindex(::DataFrame, ::Colon, ::typeof(Main.Foo.x))
Closest candidates are:
getindex(::DataFrame, ::Colon) at /Users/tomkwong/.julia/packages/DataFrames/0Em9Q/src/dataframe/dataframe.jl:306
getindex(::DataFrame, ::Colon, ::Colon) at /Users/tomkwong/.julia/packages/DataFrames/0Em9Q/src/dataframe/dataframe.jl:381
getindex(::DataFrame, ::Colon, ::Union{Signed, Symbol, Unsigned}) at /Users/tomkwong/.julia/packages/DataFrames/0Em9Q/src/dataframe/dataframe.jl:358