Message Boards Message Boards

1
|
2534 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Missing key error but it exists in the dataset

Posted 10 months ago

Suppose I have the following dataset:

dataset = Dataset[{
   <|"t" -> {-3, -2, -1, 0, 1, 2, 3}|>,
   <|"v" -> {4, -1, 3, 0, 2, -2, 5}|>}]

Why do I get a missing key when I extract a column?

dataset[[All, "v"]] // Normal
(*  {Missing["KeyAbsent", "v"], {4, -1, 3, 0, 2, -2, 5}}  *)
POSTED BY: Ehud Behar
6 Replies

Try rewriting your input expression not as a list of associations ("column" form), but in "row" form inside a single association. Note the absence of the surrounding brackets; top level head (inside Dataset) is Association, not List — see the Wolfram Language documentation on Dataset:

dataset = Dataset[<|"t"->{-3,-2,-1,0,1,2,3},"v"->{4,-1,3,0,2,-2,5}|>]; 
Normal@ %
(* <|t->{-3,-2,-1,0,1,2,3},v->{4,-1,3,0,2,-2,5}|> *)

Because the keys are in the top level, you can get key "v" value simply using:

dataset["v"]; 
Normal@ %
(* {4,-1,3,0,2,-2,5} *)

Interestingly, if you transpose this dataset, it returns the "column" form (list of associations):

dataset2 = dataset[Transpose]; 
Normal@ %
(* {<|t->-3,v->4|>,<|t->-2,v->-1|>,<|t->-1,v->3|>,<|t->0,v->0|>,<|t->1,v->2|>,<|t->2,v->-2|>,<|t->3,v->5|>} *)

Now you can interrogate dataset2 using your original query expression:

dataset2[All,"v"]; 
Normal@ %
(* {4,-1,3,0,2,-2,5} *)

Hope this helps.

POSTED BY: Carl Verdon
Posted 10 months ago

Hope this helps

That's a good explanation. Well done.

POSTED BY: Ehud Behar

You can try

dataset[Select[KeyExistsQ["v"]]]
dataset[Select[KeyExistsQ["v"]], "v"]
POSTED BY: Gianluca Gorni
Posted 10 months ago

All of the associations in a Dataset are expected to have the same keys. That is not the case here. One way would be to use pattern matching

Cases[dataset, <|"v" -> _|>]
POSTED BY: Rohit Namjoshi
Posted 10 months ago

Your dataset is malformed, or at least it's not the form you think it is. You've basically created a dataset that has two entries such that the two entries share no properties. One entry/row has a column named "t" and the other has a column named "v".

So, your query, dataset[[All, "v"]] (which could just be dataset[All, "v"], by the way), looks for the "v" column for all rows, but the first row has no "v" column (it just has a "t"). Thus the Missing[KeyAbsent,v] for the first row.

I'm not sure what you were trying to do with your dataset, so I don't know what to recommend other than to read the documentation for Dataset carefully.

POSTED BY: Eric Rimbey
Posted 10 months ago

I'm not sure what you were trying to do with your dataset

I thought that it represents a "v vs t" dataset.

Your explanation solves the confusion.

What I was looking for is rather

dataset = Dataset[{
<|"t" -> -3, "v" -> 4|>,
<|"t" -> -2, "v" -> -1|>,
<|"t" -> -1, "v" -> 3|>,
<|"t" -> 0, "v" -> 0|>,
<|"t" -> 1, "v" -> 2|>,
<|"t" -> 2, "v" -> -2|>,
<|"t" -> 3, "v" -> 5|>
}]

Thanks!

POSTED BY: Ehud Behar
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract