rwdvc
rwdvc

Reputation: 457

Julia: Using groupby with DataFramesMeta and Lazy

I'm new to Julia, and can't quite figure out how to use the groupby function with Lazy/DataFramesMeta. It seems as if Lazy has a namespace conflict with DataFrames, but I'm not sure how to resolve it.

using DataFrames, DataFramesMeta, RDatasets
#works
iris = dataset("datasets", "iris")
iris = @linq iris |>
        groupby(:Species) |>
        transform(mean_sepal_length = mean(:SepalLength))

using Lazy
#doesn't work
iris2 = dataset("datasets", "iris")
iris2 = @> begin
        iris2
        @groupby(:Species)
        @transform(mean_sepal_length = mean(:SepalLength))
end


#doesnt work
iris2 = dataset("datasets", "iris")
iris2 = @> begin
        iris2
        @DataFrames.groupby(:Species)
        @transform(mean_sepal_length = mean(:SepalLength))
end

#this works
iris2 = dataset("datasets", "iris")
iris2 = @> begin
        iris2
        @transform(mean_sepal_length = mean(:SepalLength))
end

Upvotes: 0

Views: 456

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

You have to pass qualified function name to @> and remember that there is no @groupby macro (it is a function) e.g.:

julia> using DataFrames, DataFramesMeta, RDatasets

julia> iris = dataset("datasets", "iris");

julia> a = @linq iris |>
           groupby(:Species) |>
           transform(mean_sepal_length = mean(:SepalLength));

julia> using Lazy
WARNING: using Lazy.groupby in module Main conflicts with an existing identifier.

julia> b = @> begin
           iris
           DataFrames.groupby(:Species)
           @transform(mean_sepal_length = mean(:SepalLength))
           end;

julia> a == b
true

Actually the only problem you would have is when you want to use @linq as it would not accept qualified names:

julia> a = @linq iris |>
           DataFrames.groupby(:Species) |>
           transform(mean_sepal_length = mean(:SepalLength))
ERROR: MethodError: Cannot `convert` an object of type Expr to an object of type DataFramesMeta.SymbolParameter
This may have arisen from a call to the constructor DataFramesMeta.SymbolParameter(...),
since type constructors fall back to convert methods.

a workaround is to create a variable that references the method you want:

julia> gb = DataFrames.groupby
groupby (generic function with 4 methods)

julia> a = @linq iris |>
           gb(:Species) |>
           transform(mean_sepal_length = mean(:SepalLength))

goes through.

Upvotes: 1

Related Questions