-
Notifications
You must be signed in to change notification settings - Fork 65
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
Add timedelta, timedelta64 and datetime64 plus respective conversions #509
base: main
Are you sure you want to change the base?
Changes from all commits
093bdcb
c63ab23
5a65a52
5740b59
f897600
1e8d410
b3cc79f
9dfc0dd
a3a2b97
daf9759
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,68 @@ const NUMPY_SIMPLE_TYPES = [ | |
("complex128", ComplexF64), | ||
] | ||
|
||
function pydatetime64( | ||
_year::Integer=0, _month::Integer=1, _day::Integer=1, _hour::Integer=0, _minute::Integer=0,_second::Integer=0, _millisecond::Integer=0, _microsecond::Integer=0, _nanosecond::Integer=0; | ||
year::Integer=_year, month::Integer=_month, day::Integer=_day, hour::Integer=_hour, minute::Integer=_minute, second::Integer=_second, | ||
millisecond::Integer=_millisecond, microsecond::Integer=_microsecond, nanosecond::Integer=_nanosecond | ||
) | ||
pyimport("numpy").datetime64("$(DateTime(year, month, day, hour, minute, second))") + pytimedelta64(;millisecond, microsecond, nanosecond) | ||
end | ||
function pydatetime64(@nospecialize(x::T)) where T <: Period | ||
T <: Union{Week, Day, Hour, Minute, Second, Millisecond, Microsecond} || | ||
error("Unsupported Period type: `$x::$T`. Consider using pytimedelta64 instead.") | ||
args = map(Base.Fix1(isa, x), (Day, Second, Millisecond, Microsecond, Minute, Hour, Week)) | ||
pydatetime64(map(Base.Fix1(*, x.value), args)...) | ||
end | ||
function pydatetime64(x::CompoundPeriod) | ||
x = canonicalize(x) | ||
isempty(x.periods) ? pydatetime64(Second(0)) : sum(pydatetime64, x.periods) | ||
end | ||
export pydatetime64 | ||
|
||
function pytimedelta64( | ||
_year::Integer=0, _month::Integer=0, _day::Integer=0, _hour::Integer=0, _minute::Integer=0, _second::Integer=0, _millisecond::Integer=0, _microsecond::Integer=0, _nanosecond::Integer=0, _week::Integer=0; | ||
year::Integer=_year, month::Integer=_month, day::Integer=_day, hour::Integer=_hour, minute::Integer=_minute, second::Integer=_second, microsecond::Integer=_microsecond, millisecond::Integer=_millisecond, nanosecond::Integer=_nanosecond, week::Integer=_week) | ||
pytimedelta64(sum(( | ||
Year(year), Month(month), | ||
# you cannot mix year or month with any of the below units in python | ||
# in case of wrong usage a descriptive error message will by thrown by the underlying python function | ||
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))::Int | ||
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)::Int] | ||
pyconvert_return(DateTime(_base_datetime) + T(value * count)) | ||
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)::Int] | ||
pyconvert_return(CompoundPeriod(T(value * count)) |> canonicalize) | ||
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 +116,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) | ||
let TT = (CompoundPeriod, Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, Week) | ||
Base.Cartesian.@nexprs 11 i -> pyconvert_add_rule("numpy:timedelta64", TT[i], pyconvert_rule_timedelta64, priority) | ||
end | ||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
pyimport("numpy")
the correct API call, or is that just to be used in user packages?There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
Please let me know if there's a better solution.