Hot answers tagged

40 votes
Accepted

Visualizing Type System Operations

This response defines a function called traceTypes which provides a quick-and-dirty visualization of type system operation. The function is somewhat fragile as it ...
WReach's user avatar
  • 68.3k
40 votes
Accepted

Struggling to understand Query

Short Answer The query operator for a given level can perform both descending and ascending actions, separated by the /* operator. We can tack the descending ...
WReach's user avatar
  • 68.3k
24 votes
Accepted

Query: adding additional operator changes meaning of preceding operators

This is a corner case that occurs when ascending operators are interleaved between descending operators. This case falls into an undocumented grey area. In the Details and Options section of the ...
WReach's user avatar
  • 68.3k
14 votes
Accepted

Transpose fail in 11.1 Dataset

As observed in the question, the problem lies with the type information of the dataset. Specifically, the type information for the innermost association is missing (i.e. "any type"). This causes the ...
WReach's user avatar
  • 68.3k
12 votes

Slicing an Association with named key span without normalizing?

I don't know if this is useful to you but it seems a little cleaner than your own code: ...
Mr.Wizard's user avatar
  • 269k
11 votes
Accepted

Query datasets with "variable" named slots

These work: d[Select[#[colname] > 3 &]] In the Details section of Slot, it is said that "# is equivalent to Slot[1]" so it is equivalent to write: ...
SquareOne's user avatar
  • 7,505
11 votes
Accepted

How to filter this nested list of Association

We must adjust the Select operator to account for the fact that the keys "a" and "b" are ...
WReach's user avatar
  • 68.3k
11 votes

How does Query decide the order in which the functions are applied?

@Lee's answer shows how to circumvent the problem by using RightComposition (/*) instead of ...
Lukas Lang's user avatar
  • 33.4k
10 votes

Simplifying a reverse Fold for a Query

A more direct solution with Fold using Range to reverse. With ...
Edmund's user avatar
  • 41.5k
10 votes

Query datasets with "variable" named slots

@SquareOne's answer shows the natural, idiomatic way to express the query. It exploits the fact that an association can be applied to a key to extract that key's value. A similar work-around would ...
WReach's user avatar
  • 68.3k
10 votes
Accepted

A subtlety in Query

The behaviour is by design. When we query a Dataset, the system tries to infer whether we wish the result to be a dataset itself, or just a simple value. The ...
WReach's user avatar
  • 68.3k
10 votes

Does anyone know a way to use patterns in Query?

There is no built-in facility to do this kind of transformation, but we can define our own query operator like so: ...
WReach's user avatar
  • 68.3k
10 votes
Accepted

How can I understand this example of Query taken from current Mathematica documentation?

This is "ascending" sub-query operator syntax. It operates on the entire row to produce a new row with the specified keys and values as the operator applied to the row. However, the ...
Edmund's user avatar
  • 41.5k
9 votes

A subtlety in Query

I'm not sure I'm missing something here, but as far as I can see, you should regard this as a feature. When you evaluate your Query, one of the later steps is to ...
halirutan's user avatar
  • 112k
9 votes
Accepted

Aggregation queries on lists of associations in 10.3

As confirmed by @Stefan R, it is a bug in 10.3. In the meanwhile, a possible workaround is : Merge[xx, Mean]
SquareOne's user avatar
  • 7,505
9 votes
Accepted

Does anyone know a way to use patterns in Query?

Since I am not aware of any built-ins, here is another custom solution: ...
Lukas Lang's user avatar
  • 33.4k
8 votes

Unexpected behavior of PreIncrement when querying associations

The 2nd example given under Query > Properties & Relations tells us Before being applied, Query expressions are "compiled" into ordinary compositions of ...
m_goldberg's user avatar
  • 107k
8 votes

Large Dataset query fails if column contains Missing

Extended comment converted to a non-answer, nailing down what is required to trigger this bug: Here's a simple Dataset with one key having all string values, ...
Jason B.'s user avatar
  • 67k
8 votes
Accepted

Query inside Dynamic triggers continuous high CPU load for FE

A quick fix is to add TrackedSymbols :> {searchstring} to the Dynamic. But at the end it is just another example from a ...
Kuba's user avatar
  • 136k
7 votes
Accepted

Query and Dataset Syntax Peculiarities

Let's go through it blow-by-blow... Query[DeleteMissing] @ d1 This will apply DeleteMissing to the list of associations. The documentation says: DeleteMissing[...
WReach's user avatar
  • 68.3k
7 votes

Getting slices of a nested Association via Query

This will work: db // Query[ Drop[ #, None, {3} ]& @* Transpose, 2, 1 ;; 3 ] {<|"English" -> "a", "Greek" -> "α"|>, <|"English" -> "b", "Greek" -...
gwr's user avatar
  • 13k
7 votes

Applying a function by Key pattern, preserving Association order

This should do what you want: ...
JEM_Mosig's user avatar
  • 2,943
7 votes
Accepted

Query from Association of Associations Dataset

How about: ...
Carl Woll's user avatar
  • 129k
7 votes

Select dataset rows that meet a mathematical criterion in a column

Assuming that you intend to use a Dataset with named rows (and being aware that this means that each row must have a unique name), one way is: ...
Joshua Schrier's user avatar
7 votes
Accepted

How can default valued be added in a Query?

The issue is indeed that since both defaulter and Length are ascending operators, they are applied "bottom-up". That ...
WReach's user avatar
  • 68.3k
6 votes

Getting slices of a nested Association via Query

@gwr led me to try Transpose after the first subquery as an ascending operator (/*). ...
Sascha's user avatar
  • 8,389
6 votes

Slicing an Association with named key span without normalizing?

From version 10.4 onward, we can define keySpan like this: ...
WReach's user avatar
  • 68.3k
6 votes
Accepted

Passing values from a list to Select during a Dataset query

You can use Function to proceed myDs[Select[Or @@ (Function[id, #id == id] /@ {5, 4, 1}) &], "name"] but there are ...
Kuba's user avatar
  • 136k
6 votes

Query inside Dynamic triggers continuous high CPU load for FE

You may use the TrackedSymbols option on either DynamicModule or Dynamic. From the comments ...
Edmund's user avatar
  • 41.5k
6 votes

How to pull out the underlying query syntax being used by dataset?

We can extract the query from the dataset object through pattern-matching: ...
WReach's user avatar
  • 68.3k

Only top scored, non community-wiki answers of a minimum length are eligible