Message Boards Message Boards

0
|
14529 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How to Export/Import a Dataset

Hello all,

I´ve build up a Dataset as a result of a large text-analysis. Now I want to store it on disk for later re-use (i.e. import it). I have difficulties in exporting and re-importing the dataset. When I export and re-import I get a list and and not a dataset. I tried to normalize first, but this did not work. Is there a simple way to export a dataset and import it later (and get the same dataset ;-) )?

Greetings from Germany

Mike

POSTED BY: Michael Gamer
5 Replies
Posted 9 years ago

WDX seems to work well for datasets, and gives a high level of compression as well, e.g.:

Clear[dataset, datasetwdx, datasetm]
dataset = ExampleData[{"Dataset", "StatePopulations"}];
Export["statepop.m", dataset];
Export["statepop.wdx", dataset];
FileByteCount["statepop.m"]
FileByteCount["statepop.wdx"]
datasetm = Import["statepop.m"];
datasetwdx = Import["statepop.wdx"];
Head[datasetm]
Head[datasetwdx]

Out[]= 310478
Out[]= 39346
Out[]= Dataset
Out[]= Dataset
POSTED BY: Kirk Klobe
Posted 9 years ago

WDX seems to work well for datasets, and gives a high level of compression as well, e.g.:

Clear[dataset, datasetwdx, datasetm]
dataset = ExampleData[{"Dataset", "StatePopulations"}];
Export["statepop.m", dataset];
Export["statepop.wdx", dataset];
FileByteCount["statepop.m"]
FileByteCount["statepop.wdx"]
datasetwdx = Import["statepop.wdx"];
datasetm = Import["statepop.m"];
Head[datasetwdx]
Head[datasetm]

Out[]= 310479
Out[]= 39346
Out[]= Dataset
Out[]= Dataset
POSTED BY: Kirk Klobe

Great! I first tried as .txt, but then thought the native Mathematica Package file type would make the most sense. Note that if you wanted to use .wl rather than .m (.wl is the version 10 and future file suffix for packages, though I am not sure why it has changes other than for marketing reasons) you need to explicitly tell Export that it is package format as in

exp = Export["/Users/dreiss/Desktop/dataset.wl", dataset,"Package"];

since Export apparently does not yet know that the .wl suffix is to be interpreted as a Package format as well as .m

To read in this file using Import one would then need also to explicitly specify the "Package" as in

Import["/Users/dreiss/Desktop/dataset.wl","Package"]

If one just tries

exp = Export["/Users/dreiss/Desktop/dataset.wl", dataset]

one gets an error message and the return is $Failed

I reported this as a bug since Export really should know this.

POSTED BY: David Reiss

How about exporting it into a .m file as in the following:

In[1]:= dataset = Dataset[{
    <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
    <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
    <|"a" -> 3, "b" -> "z", "c" -> {3}|>,
    <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
    <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
    <|"a" -> 6, "b" -> "z", "c" -> {}|>}];

In[2]:= exp = Export["/Users/dreiss/Desktop/dataset.m", dataset];

In[3]:= exp

Out[3]= "/Users/dreiss/Desktop/dataset.m"

In[4]:= Import[exp] - dataset

Out[4]= 0

In[5]:= Head[Import[exp]]

Out[5]= Dataset

In[6]:= Get[exp] - dataset

Out[6]= 0

In[7]:= Head[Get[exp]]

Out[7]= Dataset
POSTED BY: David Reiss

Hi David,

thanks for your help - which is really one :-) I tried to export in various formats ("List" etc.) no way. The text, as a part of the data, is German text, so we have all the "Umlaute" {ä,ö,ü...}. When importing the are transformed and so on. Exporting to a "*.m" file seems to work. Thank you!

Greetings Mike

POSTED BY: Michael Gamer
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