When using the Wolfram Client Library for Python in a Python program there appears to be no built-in function to convert a DateObject in to a Python datetime object.

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr

wolfSession = WolframLanguageSession()

With date_values as DateObjects in Python

dt = wl.DateObject([2019,1,1,0,15]);
        wl.DatePlus(dt, [1*15, 'Minute']),
        wl.Quantity(15, 'Minute')

[DateObject[[2019, 1, 1, 0, 15], 'Minute', 'Gregorian', -3.0], DateObject[[2019, 1, 1, 0, 30], 'Minute', 'Gregorian', -3.0]]

The only way I have found to convert these is by parsing the "ISODateTime" string from DateString in Python.

import iso8601

    lambda d: iso8601.parse_date(d), 

[datetime.datetime(2019, 1, 1, 0, 15, tzinfo=datetime.timezone.utc), datetime.datetime(2019, 1, 1, 0, 30, tzinfo=datetime.timezone.utc)]

This seems verbose considering many types automatically convert between a Wolfram session and Python (and vice versa).

Am I missing a built-in function to do this? If not, is there a terse method of converting a list of Wolfram DateObjects into Python datetime objects in Python?

  • 1
    $\begingroup$ You should look in the WolframClientLibrary code, esp. here. The library appears to provide a way to automatically convert types over by registering a consumer object. By knowing the structure of a Mathematica DateObject you can directly construct the datetime one. I know this isn't terse, but it is extensible and really the way things like this ought to be done in general. $\endgroup$ – b3m2a1 May 28 '19 at 18:43
  • $\begingroup$ @b3m2a1 Interesting. I think I will give it a try. $\endgroup$ – Edmund May 28 '19 at 23:10

Your Answer

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

Browse other questions tagged or ask your own question.