Query.jl, referencing `_.key` in `@groupby |> @map`

I followed the recording of the Queryverse tutorial at JuliaCon by @davidanthoff and now get errors when I use similar queries. In particular, I want to use the _.key shortcut in this query (see full example):

data |>
    @groupby(_.symbol) |>
    @map({Symbol=_.key, Total=sum(_.price)})
--
type NamedTuple has no field key

I’m guessing the issue here is that I am using Julia 1.0 where NamedTuple is part of the language, while the tutorial was based on Julia 0.6 with NamedTuple from a 3rd party package?

In the Gist, I also wonder how to work around that issue.

Hi,

I noticed that you used @map({Symbol =_.key, .... and key doesn’t exist in the tuple, so just changing that to @map({Symbol =_.symbol, ... works.

As for a complete example, this those what you want

data |>
    @groupby(_.symbol) |>  
    @map({Symbol = _.symbol[1], Total=sum(_.price)})

This version also works.

x = data |>
    @groupby(_.symbol) |>  
    @map({ Symbol = key(_), Total=sum(_.price)})

Yes, something like this is also what I came up with as a workaround.
It’s still strange that the example from the tutorial would not work.

Thanks, I did not know about this syntax, which is also nicer than _.key.

This is one of the breaking changes for the julia 1.0 version: you now generally have to access the key with the key function. That opens up the .foo syntax to access the columns of a group:

using Queryverse

df = DataFrame(A=[:a, :a, :b, :b], B=[1.,2.,3.,4.])

df |> @groupby(_.A) |> @map({A=key(_), B=sum(_.B)})

Note how I can now use _.B to access the whole B column of a given group _.

2 Likes

I could not make this work if grouping was done with more than one variable, like in groupby(_.A, _.B), or even with a third column C.

Here is how you do that:

df |>
@groupby({_.A, _.B, _.c}) |>
@map({key(_)..., D=sum(_.D), E=mean(_.E)})

or something like that.

The trick is to construct a new named tuple value by which things get grouped in the @groupby command, and then key(_) will be a named tuple itself.