-
Notifications
You must be signed in to change notification settings - Fork 69
Add timedelta, timedelta64 and datetime64 plus respective conversions #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
093bdcb
c63ab23
5a65a52
5740b59
f897600
1e8d410
b3cc79f
9dfc0dd
a3a2b97
daf9759
60e0daa
7391b8d
8f28567
46efe53
d36c113
66459c6
3c51b54
fac1ef8
d00a788
5abcf1d
34f35ce
9864173
3148bae
94b47df
f66f526
e9277ef
d93e99a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,66 @@ const NUMPY_SIMPLE_TYPES = [ | |
("complex128", ComplexF64), | ||
] | ||
|
||
function pydatetime64( | ||
_year::Int=0, _month::Int=1, _day::Int=1, _hour::Int=0, _minute::Int=0,_second::Int=0, _millisecond::Int=0, _microsecond::Int=0, _nanosecond::Int=0; | ||
year::Int=_year, month::Int=_month, day::Int=_day, hour::Int=_hour, minute::Int=_minute, second::Int=_second, | ||
millisecond::Int=_millisecond, microsecond::Int=_microsecond, nanosecond::Int=_nanosecond | ||
) | ||
pyimport("numpy").datetime64("$(DateTime(year, month, day, hour, minute, second))") + pytimedelta64(;millisecond, microsecond, nanosecond) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw similar calls at different places in the package, so I took this approach. But I also wouldn't know how to code a timedelta64 without calling pyimport. |
||
end | ||
function pydatetime64(@nospecialize(x::T)) where T <: Period | ||
T <: Union{Week, Day, Hour, Minute, Second, Millisecond, Microsecond} || | ||
error("Unsupported Period type: ", "Year, Month and Nanosecond are not supported, consider using pytimedelta64 instead.") | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
args = T .== (Day, Second, Millisecond, Microsecond, Minute, Hour, Week) | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pydatetime64(x.value .* args...) | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to rewrite this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sum is dammed fast (16ns), and I couldn't beat it with a different version. |
||
end | ||
function pydatetime64(x::CompoundPeriod) | ||
x = canonicalize(x) | ||
isempty(x.periods) ? pydatetime64(Second(0)) : sum(pydatetime64.(x.periods)) | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
export pydatetime64 | ||
|
||
function pytimedelta64( | ||
_year::Int=0, _month::Int=0, _day::Int=0, _hour::Int=0, _minute::Int=0, _second::Int=0, _millisecond::Int=0, _microsecond::Int=0, _nanosecond::Int=0, _week::Int=0; | ||
year::Int=_year, month::Int=_month, day::Int=_day, hour::Int=_hour, minute::Int=_minute, second::Int=_second, microsecond::Int=_microsecond, millisecond::Int=_millisecond, nanosecond::Int=_nanosecond, week::Int=_week) | ||
pytimedelta64(sum(( | ||
Year(year), Month(month), # you cannot mix year or month with any of the below units in python, the error will be thrown by `pytimedelta64(::CompoundPeriod)` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment
Should be presented to the user as a descriptive error message rather than a comment in the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the comment isn't clear enough. |
||
Day(day), Hour(hour), Minute(minute), Second(second), Millisecond(millisecond), Microsecond(microsecond), Nanosecond(nanosecond), Week(week)) | ||
)) | ||
end | ||
function pytimedelta64(@nospecialize(x::T)) where T <: Period | ||
index = findfirst(==(T), (Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, T)) | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unit = ("Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "")[index] | ||
pyimport("numpy").timedelta64(x.value, unit) | ||
end | ||
function pytimedelta64(x::CompoundPeriod) | ||
x = canonicalize(x) | ||
isempty(x.periods) ? pytimedelta64(Second(0)) : sum(pytimedelta64.(x.periods)) | ||
end | ||
export pytimedelta64 | ||
|
||
function pyconvert_rule_datetime64(::Type{DateTime}, x::Py) | ||
unit, count = pyconvert(Tuple, pyimport("numpy").datetime_data(x)) | ||
value = reinterpret(Int64, pyconvert(Vector, x))[1] | ||
units = ("Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns") | ||
types = (Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond) | ||
T = types[findfirst(==(unit), units)] | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pyconvert_return(DateTime(_base_datetime) + T(value * count)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to other comment – you should write this using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, tested with a julia function calls and found that the julia part is around 25ns, whereas the python call is around 1.5microsecond |
||
end | ||
|
||
function pyconvert_rule_timedelta64(::Type{CompoundPeriod}, x::Py) | ||
unit, count = pyconvert(Tuple, pyimport("numpy").datetime_data(x)) | ||
value = reinterpret(Int64, pyconvert(Vector, x))[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought, pyconvert creates a new Julia Vector which is not mapped onto Python data. If that would be the case, we'd need to wrap the vector by a |
||
units = ("Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns") | ||
types = (Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond) | ||
T = types[findfirst(==(unit), units)] | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pyconvert_return(CompoundPeriod(T(value * count)) |> canonicalize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The proper way to do this would be to use |
||
end | ||
|
||
function pyconvert_rule_timedelta64(::Type{T}, x::Py) where T<:Period | ||
pyconvert_return(convert(T, pyconvert_rule_timedelta64(CompoundPeriod, x))) | ||
end | ||
|
||
function init_numpy() | ||
for (t, T) in NUMPY_SIMPLE_TYPES | ||
isbool = occursin("bool", t) | ||
|
@@ -54,4 +114,14 @@ function init_numpy() | |
iscomplex && pyconvert_add_rule(name, Complex, rule) | ||
isnumber && pyconvert_add_rule(name, Number, rule) | ||
end | ||
|
||
priority = PYCONVERT_PRIORITY_ARRAY | ||
pyconvert_add_rule("numpy:datetime64", DateTime, pyconvert_rule_datetime64, priority) | ||
for T in (CompoundPeriod, Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, Week) | ||
pyconvert_add_rule("numpy:timedelta64", T, pyconvert_rule_timedelta64, priority) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Julia is unlikely to unroll this loop, you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried my best, but I'm not sure how to test whether this will speed up things |
||
|
||
priority = PYCONVERT_PRIORITY_CANONICAL | ||
pyconvert_add_rule("numpy:datetime64", DateTime, pyconvert_rule_datetime64, priority) | ||
pyconvert_add_rule("numpy:timedelta64", Nanosecond, pyconvert_rule_timedelta, priority) | ||
end |
Uh oh!
There was an error while loading. Please reload this page.