Message Boards Message Boards

0
|
2711 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Message[ ] fail (when applying a function to a dataset row)?

Posted 3 years ago

If I define a simple dataset and a simple function to apply to that data set:

data = Dataset[{<|"a" -> 1 , "b" -> 2, "c" -> 3|>,
                        <|"a" -> 4 , "b" -> 5, "c" -> -6|>,
                        <|"a" -> 7 , "b" -> 8, "c" -> 9|>}];
myValue[assoc_] := Module[{a = assoc},
  myValue::errneg = "`1` is negative";
  If[a["c"] < 0, Message[myValue::errneg, ToString[a["c"]]]; 
   Return[a["b"]], Return[a["c"]]]
  ]

there is a failure behavior of the Message function I do not understand when applying the function to all rows of the dataset:

data[All, myValue]

The result is the myValue::errneg message wrapped in a Failure:

Failure[myValue, 
Association[
 "MessageTemplate" :> MessageName[myValue, "errneg"], 
  "MessageParameters" -> {"-6"}]]

This failure message is not returned when calling the myValue function with an association:

myValue[data[2]] 
myValue::errneg: -6 is negative

Is there an explanation for this behavior (why is the call to Message generating a failure)?

POSTED BY: Andrew
2 Replies
Posted 3 years ago

Thanks Mike. That fixed the problem!

POSTED BY: Andrew
Posted 3 years ago

Andrew:

It seems that the Query operator, by default, treats Messages as failures. You can change the default behavior using FailureAction.

data[All, myValue, FailureAction -> None]
POSTED BY: Mike Besso
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