6
$\begingroup$

Consider the following dataset:

dat = Dataset[<| "A" -> 1, "B" -> 2|>];
ExportString[dat, "CSV"]

It exports perfectly, with the expected result:

(* "A",1
   "B",2 *)

The above also works fine if the cells of the dataset are strings:

dat = Dataset[<| "A" -> "1", "B" -> "2"|>];

However, if the cells are reals (only one of them seems to be enough), then

dat = Dataset[<| "A" -> 1.0, "B"-> 2.0 |>];
ExportString[dat, "CSV"]

returns $Failed, with the following message:

Dataset::exportfail: Dataset of the given form could not be exported as format "CSV".

The same issue appears when using Export rather than ExportString. I am currently using ToString to convert all cells to strings before exporting. Is this a bug?

I am using OS X, Mathematica version 11.

$\endgroup$
  • 2
    $\begingroup$ Looks like a bug to me. $\endgroup$ – m_goldberg Sep 10 '16 at 16:11
3
$\begingroup$

In version 11.0.0.0, a detailed examination of the traced execution of ExportString[dat, "CSV"] reveals that the error occurs due to a bug in the internal function Dataset`ImportExport`PackagePrivate`txt.

This function is supposed to convert values of various types to strings. But we find that it has problems with real numbers:

Dataset;

Dataset`ImportExport`PackagePrivate`txt[1]
(* 1 *)

Dataset`ImportExport`PackagePrivate`txt[1.0]
(* GeneralUtilities`TextString`PackagePrivate`fmtReal[1., 10] *)

We can inspect the definitions of txt, looking for the definition applicable for real numbers:

Needs["GeneralUtilities`"]
PrintDefinitions[Dataset`ImportExport`PackagePrivate`txt]

(*
   ...
   txt[r_Real | r_Rational] := fmtReal[r, 10];
   ...
*)

However, if we look at the definitions of fmtReal, we find that the only definition expects one argument, not two:

PrintDefinitions[GeneralUtilities`TextString`PackagePrivate`fmtReal]

(*
   fmtReal[r_] := FromCharacterCode@...
*)

Patch

If we are willing to apply an unsanctioned patch ourselves, we can fix the bug:

Dataset;

Dataset`ImportExport`PackagePrivate`txt[r_Real | r_Rational] :=
  GeneralUtilities`TextString`PackagePrivate`fmtReal[r];

dat = Dataset[<| "A" -> 1.0, "B"-> 2.0 |>];
ExportString[dat, "CSV"]

(*
   "A",1.
   "B",2.
*)

Note that this patch must be applied after the dataset packages have been auto-loaded. The otherwise apparently useless expression Dataset; forces the auto-loading to happen.

$\endgroup$
  • $\begingroup$ Thanks. I have also submitted a bug report. $\endgroup$ – ecoxlinux Sep 11 '16 at 2:46
  • $\begingroup$ @WReach I also get this error but the Stack Trace just shows error.... $\endgroup$ – SumNeuron Jan 18 '17 at 14:38
  • $\begingroup$ @SumNeuron Yes, the stack trace does not show much. Try wading through the output given by Select[UpValues[Dataset], Not@*FreeQ[ExportString]] and TracePrint[ Dataset`ImportExport`PackagePrivate`datasetExportString[dat, "CSV"]]. Debugging system internals is tricky (especially Dataset), but alas a meaty discussion of the techniques will not fit into comment boxes :D $\endgroup$ – WReach Jan 18 '17 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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