0
$\begingroup$

Suppose I have the following Dataset, one of whose columns is a nested Dataset:

ClearAll[dat];
dat = Dataset[{
    <|
    "a" -> 2,
    "b" -> 3,
    "c" -> Dataset[{<|"c1" -> 2, "c2" -> 4|>,  <|"c1" -> 3, "c2" -> 6|>}]
    |>,
    <|
    "a" -> 3,
    "b" -> 4,
    "c" -> Dataset[{<|"c1" -> 3, "c2" -> 6|>, <|"c1" -> 4, "c2" -> 8|>}]
    |>,
    <|
    "a" -> 4,
    "b" -> 1,
    "c" -> Dataset[{<|"c1" -> 4, "c2" -> 8|>, <|"c1" -> 1, "c2" -> 2|>}]
    |>,
    <|
    "a" -> 4,
    "b" -> 2,
    "c" -> Dataset[{<|"c1" -> 4, "c2" -> 8|>, <|"c1" -> 2, "c2" -> 4|>}]
    |>
}];

This gives me the following:

dat

Now I want to transform dat in the following way:

dat // Query[All, <|
    "A" -> "a", "B" -> "b",
    "T2" -> (Key["c"] /* (#[All, "c2"] &) /* Normal /* Total)
|>]

This gives me the expected result:

T2_1

However, when I run the following:

dat // Query[All, <|
    "A" -> "a", "B" -> "b",
    "T2" -> (Key["c"] /* Query[All, "c2"] /* Normal /* Total)
|>]

I get zeros in the last column instead:

T2_2

So, what gives? Why does Query not work for the inner Dataset? Is this just an idiosyncrasy with which I must live? Or is there are deeper model of execution which I am failing to understand?

$\endgroup$
  • $\begingroup$ You are missing a # in the last evaluation $\endgroup$ – Giovanni Baez Jul 5 '18 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.