8
$\begingroup$

i'm trying to get familiar with the new Dataset-stuff in Mathematica 10. As it will (hopefully) provide some benefit for me, I have started to convert my app to move from list and list of rules to Associations and Datasets.

Starting point for me is an XML object, which is converted to a Dataset now. I was hoping to reduce the programming code by e.g. applying functions to multiple columns at once.

This is an example Dataset:

testset = Dataset[{<|"Element" -> "A", "a" -> "529251", "b" -> "520358"|>, <|
"Element" -> "B", "a" -> "51"|>, <|"Element" -> "C", 
"a" -> "177"|>, <|"Element" -> "L", "a" -> "125"|>, <|
"Element" -> "S", "a" -> "1343"|>}]

This gives:

dataset

As the XML sometimes contains integers and reals in scientific form, I'm using

Internal`StringToDouble

to convert the stuff:

fnStr2Real[x_,r_:0]:=If[Head[x]===Missing,r,Internal`StringToDouble[x]]
SetAttributes[fnStr2Real, {Listable}];
testset[fnStr2Real, {"a", "b"}]
(* Trace yields *)
(* Query[fnStr2Real, {"a", "b"}][testset] *)

The issue: there is some kind of rounding in the resulting recordset:

result

If I apply my function directly to my list or association, everything is fine:

Function[x, fnStr2Real[#[x] & /@ Normal[testset]]] /@ {"a", "b"}
(* {{529251., 51., 177., 125., 1343.}, {520358., 0, 0, 0, 0}} *)

I know that Internal`StringToDouble is an internal function, but I found several posts in this forum using it, and I really like it!

Is there an issue with M10, Dataset and Internal`StringToDouble?

Are there any other robust string coversion functions availabe which can handle integers, reals and reals in scientific form inside a dataset? Thanks!

$\endgroup$
4
  • 1
    $\begingroup$ Sorry, you need to set the Listable attribute: SetAttributes[fnStr2Real, {Listable}]; $\endgroup$
    – 32u-nd
    Jul 17, 2014 at 9:51
  • $\begingroup$ Why don't you use ToExpression for the conversion? You could check before conversion whether the string contains numeric data using StringMatchQ. $\endgroup$ Jul 17, 2014 at 10:41
  • $\begingroup$ InternalStringToDouble is fine. Check InternalStringToDouble /@ {"1234", "1234.", "1234.567", "1.234e+3", "1.23456e+3", "0.5664", "5.664e-1", "5.664e-4"} $\endgroup$
    – 32u-nd
    Jul 17, 2014 at 11:14
  • 1
    $\begingroup$ @SjoerdC.deVries ToExpression is very slow compared to Internal`StringToDouble. See this $\endgroup$
    – Murta
    Jul 17, 2014 at 13:19

1 Answer 1

5
$\begingroup$

I don't yet know where the rounding is coming from but it appears to be occurring during formatting.

Consider:

testset[fnStr2Real, {"a", "b"}] // Normal // Normal
{{"a" -> 529251., "b" -> 520358.}, {"a" -> 51., "b" -> 0},
 {"a" -> 177., "b" -> 0}, {"a" -> 125., "b" -> 0}, {"a" -> 1343., "b" -> 0}}

Actually the rounding seems to be a standard part of the formatting of Dataset:

Dataset[<|a -> 123456.|>]

enter image description here

$\endgroup$
10
  • $\begingroup$ Yes, you're right; interesting. It also appears correctly in the FullForm. So I don't need to focus on this and can continue with my work. Everything is fine; it's just a matter of vizualisation $\endgroup$
    – 32u-nd
    Jul 17, 2014 at 10:11
  • $\begingroup$ @akm I think that's correct, but it's still surprising. I haven't yet read about Datasets so this may be plainly documented. Perhaps there is an Option that controls it. I'll try to remember to update this post later as I learn more. $\endgroup$
    – Mr.Wizard
    Jul 17, 2014 at 10:12
  • $\begingroup$ Maybe, the programmers have remembered this: (stat.washington.edu/pds/stat423/Documents/…), which is actually good, but should be documented. $\endgroup$
    – 32u-nd
    Jul 17, 2014 at 10:17
  • 5
    $\begingroup$ @Mr.Wizard It's neither documented nor controllable. The formatting of Dataset is quite limited at the moment, there are several ways I wish to expand it and make it more interactive, so making it controllable now is premature and would probably lock the design to something sub-optimal. $\endgroup$ Jul 17, 2014 at 13:46
  • 1
    $\begingroup$ @TaliesinBeynon, why not give options for formatting as there are for FailureAction in Query. Is it safe to Unprotect and roll implement custom formatters? eg w/ scrollbars for large data. $\endgroup$ Sep 23, 2014 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.