Group Abstract Group Abstract

Message Boards Message Boards

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

Missing key error but it exists in the dataset

Posted 2 years 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
POSTED BY: Carl Verdon
Posted 2 years 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 2 years ago
POSTED BY: Rohit Namjoshi
Posted 2 years 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 2 years 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