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


#1

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.


#2

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)})

#3

This version also works.

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

#4

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.


#5

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


#6

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 _.