You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conversion from Python datetime to Julia DateTime fails #265
Hmm I think this is because a Julia DateTime is to millisecond precision whereas a Python datetime is to microsecond precision, and PythonCall currently refuses to do the conversion unless the microseconds are zero.
I could add a conversion rule so that if you specifically ask for a DateTime then it will do the lossy conversion.
In the meantime, you can do this to get the time to the nearest second:
using CondaPkg
using PythonCall
using Dates
datetime =pyimport("datetime")
t = datetime.datetime.now()
# t = t.replace(microsecond=0) # nearest second
t = t.replace(microsecond=(t.microsecond//1000)*1000)
pyconvert(DateTime, t)
This issue has been marked as stale because it has been open for 30 days with no activity. If the issue is still relevant then please leave a comment, or else it will be closed in 7 days.
MethodError: no method matching //(::Py, ::Int64)
Closest candidates are:
//(!Matched::Integer, ::Integer)
@ Base rational.jl:62
//(!Matched::Rational, ::Integer)
@ Base rational.jl:64
//(!Matched::Complex, ::Real)
@ Base rational.jl:78
I tried to add pyconvert(t.microsecond, Int) but that does not work either
MethodError: no method matching pyconvert(::Py, ::Type{Int64})
Closest candidates are:
pyconvert(!Matched::Type{T}, ::Any) where T
@ PythonCall ~/.julia/packages/PythonCall/qTEA1/src/convert.jl:370
pyconvert(!Matched::Type{T}, ::Any, !Matched::Any) where T
@ PythonCall ~/.julia/packages/PythonCall/qTEA1/src/convert.jl:371
But honestly, converting a Python datetime.datetime to DateTime is mundane enough that it should work out of the box. The truncation from microseconds to milliseconds is unavoidable, and the programmer who needs to do that conversion will bite the bullet.
The pyconvert rules are lossless by default, hence refusing to truncate. However there's an argument that small losses in precision are OK (just as convert(Float16, 1.2) loses precision) so maybe it's OK in this case. I've just been burned too many times by Python happily converting floats to ints.
In the future, I'm planning to add keyword args to pyconvert to allow more flexible conversion, so that e.g. pyconvert(DateTime, t; truncate=true) will allow a loss of precision.
Activity
cjdoris commentedon Feb 9, 2023
Hmm I think this is because a Julia
DateTimeis to millisecond precision whereas a Pythondatetimeis to microsecond precision, and PythonCall currently refuses to do the conversion unless the microseconds are zero.I could add a conversion rule so that if you specifically ask for a
DateTimethen it will do the lossy conversion.luc-j-bourhis commentedon Feb 9, 2023
Such a conversion would work fine for what I am doing. Thanks.
cjdoris commentedon Feb 10, 2023
In the meantime, you can do this to get the time to the nearest second:
github-actions commentedon Sep 1, 2023
This issue has been marked as stale because it has been open for 30 days with no activity. If the issue is still relevant then please leave a comment, or else it will be closed in 7 days.
luc-j-bourhis commentedon Sep 1, 2023
Your proposition gives me a stacktrace:
I tried to add
pyconvert(t.microsecond, Int)but that does not work eitherBut honestly, converting a Python
datetime.datetimeto DateTime is mundane enough that it should work out of the box. The truncation from microseconds to milliseconds is unavoidable, and the programmer who needs to do that conversion will bite the bullet.Note: this is now with PythonCall 0.9.14
cjdoris commentedon Sep 13, 2023
It's
pyconvert(Int, t.microsecond).The
pyconvertrules are lossless by default, hence refusing to truncate. However there's an argument that small losses in precision are OK (just asconvert(Float16, 1.2)loses precision) so maybe it's OK in this case. I've just been burned too many times by Python happily converting floats to ints.In the future, I'm planning to add keyword args to
pyconvertto allow more flexible conversion, so that e.g.pyconvert(DateTime, t; truncate=true)will allow a loss of precision.