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 ...
user avatar
  • 67k
39 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 ...
user avatar
  • 67k
23 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 ...
user avatar
  • 67k
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 ...
user avatar
  • 67k
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: ...
user avatar
  • 264k
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: ...
user avatar
  • 7,195
11 votes

10.0.2 breaks a recursive "trie" query

There appears to be a bug in the type inference mechanism here. If you evaluate <<Dataset` <<TypeSystem` and then ...
user avatar
  • 2,106
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 ...
user avatar
  • 67k
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 ...
user avatar
  • 23.4k
10 votes
Accepted

Applying a function to a Dataset column conditional on another slot's value

Here is one way: data[All, MapAt[foo, #, #PLATFORM /. {"Dropbox" -> "LOCATION", _ -> {}}] &] Or, if one prefers If: ...
user avatar
  • 67k
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 ...
user avatar
  • 67k
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 ...
user avatar
  • 67k
10 votes

Simplifying a reverse Fold for a Query

A more direct solution with Fold using Range to reverse. With ...
user avatar
  • 39.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: ...
user avatar
  • 67k
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 ...
user avatar
  • 39.3k
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]
user avatar
  • 7,195
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 ...
user avatar
  • 111k
9 votes

Problem constructing Association tables with some operators

This appears to be another example of the WRI-acknowledged bug described in Possible bug involving Dataset/Query and RightComposition. In short, the query is being "compiled" into an expression that ...
user avatar
  • 67k
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: ...
user avatar
  • 23.4k
8 votes
Accepted

Filtering Query Nested Data (Dataset and Association)

The following query will return the desired result: ...
user avatar
  • 67k
8 votes

Applying a function to a Dataset column conditional on another slot's value

Okay, this question is already answered, but I want to add my 'preferred way' here: First, we define a new operator: ...
user avatar
8 votes

Applying a function to a Dataset column conditional on another slot's value

Perhaps, there is a better one, but here is one that comes to mind: ClearAll[transform]; transform["Dropbox"] := foo; transform[_] := Identity; and then ...
user avatar
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, ...
user avatar
  • 62.6k
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 ...
user avatar
  • 106k
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 ...
user avatar
  • 132k
7 votes

Applying a function to a Dataset column conditional on another slot's value

Here is another relatively straightforward approach: data[All, <|#, "LOCATION" -> If[#PLATFORM == "Dropbox", foo[#LOCATION], #LOCATION]|> &] Or ...
user avatar
  • 1,126
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[...
user avatar
  • 67k
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" -...
user avatar
  • 12.3k
7 votes

Applying a function by Key pattern, preserving Association order

This should do what you want: ...
user avatar
  • 2,791
7 votes
Accepted

Query from Association of Associations Dataset

How about: ...
user avatar
  • 123k

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