3
$\begingroup$

I have a number of long lists of DateObjects paired with values. An example data element looks like this:

enter image description here

It takes several hours of processing time to generate these lists and as I can reuse them, I want to save them so I can Import them as needed rather than recalculating them.

I tried exporting the data lists:

Export[".../Index model/sp.CSV", sp,"CSV"];

Upon then:

Import[".../Index model/sp.CSV","CSV"]

I get elements that look like the following:

{"DateObject[{1977, 2, 15, 0, 0, 0.}, \"Instant\", \"Gregorian\", \
-5.]", 101.04}

Which does not give me an immediately computable DateObject.

I appreciate that I can derive a DateObject from this imported data, but that defeats some of the purpose in exporting the data in the first place.

Does someway exist so I can Export DateObjects that stay DateObjects on Import?

$\endgroup$
1
  • $\begingroup$ ToExpression can restore it after import. $\endgroup$
    – Syed
    Feb 18, 2023 at 18:04

1 Answer 1

3
$\begingroup$
$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global`*"]

SeedRandom[1234];

dates = RandomDate[DateObject[List[2022], "Year"], 5] // Sort

enter image description here

Export["test.tsv", dates, "Text"]

(* "test.tsv" *)

dates2 = Import["test.tsv"] // ToExpression // Flatten;

They are identical,

dates === dates2

(* True *)
$\endgroup$

Your Answer

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

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