Reputation: 1492
In Julia I want to declare custom conversion rules to wrap up computations that are done in Python.
As a simple example it does not appear that ZonedDateTime
s convert into python correctly so I wanted to write type conversions for them. As an example if I write:
using TimeZones, Dates
using PyCall
py"""
import datetime as dt
from dateutil import tz
import pandas as pd
def print_time(zdt):
print(type(zdt))
if isinstance(zdt, dt.datetime):
print(zdt.tzinfo)
def give_zdt():
return dt.datetime(2020, 1, 1, 0, 0, 0, 0, tz.gettz("Australia/Sydney"))
def give_series():
return pd.Series([dt.datetime(2020, 1, 1, 0, 0, 0, 0), dt.datetime(2020, 1, 2, 0, 0, 0, 0)])
"""
I want to write type conversions to fix the following issues:
# This should show a dt.datetime with a valid timezone.
a = py"print_time"(ZonedDateTime(2020, 1, 1, 0, 0, 0, 0, tz"UTC"))
# <class 'PyCall.jlwrap'>
# This should return a ZonedDateTime
b = py"give_zdt"()
typeof(b) # DateTime
# This should return a Vector{DateTime}
c = py"give_series"()
typeof(c) # PyObject
To do this it seems like I need to write Py(::T)
functions to map from Julia to python and add conversion rules to go from Python to Julia. I can't find any examples of how to do this in the context of a simple script. It also seems complicated in that datetime.datetime
in python should map to either DateTime
or ZonedDateTime
depending on if tzinfo has been declared.
Can anyone with a good knowledge of PyCall provide a working example of how to do this?
Upvotes: 0
Views: 31