# Failure when exporting dataset with real-valued cells

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.

• Looks like a bug to me. – m_goldberg Sep 10 '16 at 16:11

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 DatasetImportExportPackagePrivatetxt.

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

Dataset;

DatasetImportExportPackagePrivatetxt[1]
(* 1 *)

DatasetImportExportPackagePrivatetxt[1.0]
(* GeneralUtilitiesTextStringPackagePrivatefmtReal[1., 10] *)


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

Needs["GeneralUtilities"]
PrintDefinitions[DatasetImportExportPackagePrivatetxt]

(*
...
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[GeneralUtilitiesTextStringPackagePrivatefmtReal]

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


Patch

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

Dataset;

DatasetImportExportPackagePrivatetxt[r_Real | r_Rational] :=
GeneralUtilitiesTextStringPackagePrivatefmtReal[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.

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