Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
093bdcb
add support for `@py from <module> import`
Feb 21, 2022
c63ab23
Merge branch 'cjdoris:main' into main
hhaensel Jul 5, 2023
5a65a52
Merge branch 'main' of https://github.com/hhaensel/PythonCall.jl
hhaensel Jun 16, 2024
5740b59
support timedelta, timedelta64, datetime64 and respective conversions
hhaensel Jun 16, 2024
f897600
fix week kw in pytimedelta64, typo (space) in builtins
hhaensel Jun 16, 2024
1e8d410
correct handling of count in 64-bit conversion rules
Jun 17, 2024
b3cc79f
Merge remote-tracking branch 'origin/main' into hh-timedelta64
hhaensel Jul 28, 2024
9dfc0dd
Apply suggestions from code review
hhaensel Sep 6, 2024
a3a2b97
Apply suggestions from code review part II
hhaensel Sep 6, 2024
daf9759
reviewers suggestions part III
hhaensel Sep 6, 2024
60e0daa
Merge branch 'JuliaPy:main' into hh-timedelta64
hhaensel Jan 19, 2025
7391b8d
add tests for pytimedelta
Jan 19, 2025
8f28567
fix micro/millisecond in pytimedelta,
Jan 20, 2025
46efe53
add tests for pytimedelta, pytimedelta64 and conversion of pytimedelt…
Jan 20, 2025
d36c113
fix pytimdelta(years/months=0), add pydatetime64(::Union{Date, DateTi…
hhaensel Jan 21, 2025
66459c6
add tests for pytimedelta64, pydatetime64
hhaensel Jan 21, 2025
3c51b54
support unitless timedelta64, keep unit per default, add keyword cano…
Jan 21, 2025
fac1ef8
add tests for timedelta64 canonicalize
Jan 21, 2025
d00a788
Merge branch 'JuliaPy:main' into hh-timedelta64
hhaensel Jan 21, 2025
5abcf1d
add CondaPkg and DataFrames as extras in Project.toml
Jan 21, 2025
34f35ce
fix pandas testing
Jan 20, 2025
9864173
fix compat with julia < 1.8
Jan 21, 2025
3148bae
specialize Base.convert(::<:Period, CompoundPeriod) for julia < 1.8
Jan 21, 2025
94b47df
change CondaPkg environment
Jan 21, 2025
f66f526
fix test/Project.toml, adapt runtests.jl
Jan 21, 2025
e9277ef
change order of args of pytimedelta; add docstrings for pytimedelta, …
Feb 19, 2025
d93e99a
remove `@py`method for python-style imports
Feb 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
support unitless timedelta64, keep unit per default, add keyword cano…
…nicalize, add global setting CANONICALIZE_TIMEDELTA64 for rule conversion
  • Loading branch information
hhaensel committed Jan 21, 2025
commit 3c51b54cd69054f1e605de63b28605a0d64d549f
55 changes: 40 additions & 15 deletions src/Convert/numpy.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const CANONICALIZE_TIMEDELTA64 = Ref(false)

struct pyconvert_rule_numpysimplevalue{R,S} <: Function end

function (::pyconvert_rule_numpysimplevalue{R,SAFE})(::Type{T}, x::Py) where {R,SAFE,T}
Expand Down Expand Up @@ -47,33 +49,54 @@ end
export pydatetime64

function pytimedelta64(
_years::Union{Nothing,Integer}=nothing, _months::Union{Nothing,Integer}=nothing, _days::Integer=0, _hours::Integer=0, _minutes::Integer=0, _seconds::Integer=0, _milliseconds::Integer=0, _microseconds::Integer=0, _nanoseconds::Integer=0, _weeks::Integer=0;
years::Union{Nothing,Integer}=_years, months::Union{Nothing,Integer}=_years, days::Integer=_days, hours::Integer=_hours, minutes::Integer=_minutes, seconds::Integer=_seconds, microseconds::Integer=_microseconds, milliseconds::Integer=_milliseconds, nanoseconds::Integer=_nanoseconds, weeks::Integer=_weeks)
year_or_month_given = (years !== nothing || months !== nothing)
_years::Union{Nothing,Integer}=nothing, _months::Union{Nothing,Integer}=nothing,
_days::Union{Nothing,Integer}=nothing, _hours::Union{Nothing,Integer}=nothing,
_minutes::Union{Nothing,Integer}=nothing, _seconds::Union{Nothing,Integer}=nothing,
_milliseconds::Union{Nothing,Integer}=nothing, _microseconds::Union{Nothing,Integer}=nothing,
_nanoseconds::Union{Nothing,Integer}=nothing, _weeks::Union{Nothing,Integer}=nothing;
years::Union{Nothing,Integer}=_years, months::Union{Nothing,Integer}=_months,
days::Union{Nothing,Integer}=_days, hours::Union{Nothing,Integer}=_hours,
minutes::Union{Nothing,Integer}=_minutes, seconds::Union{Nothing,Integer}=_seconds,
milliseconds::Union{Nothing,Integer}=_milliseconds, microseconds::Union{Nothing,Integer}=_microseconds,
nanoseconds::Union{Nothing,Integer}=_nanoseconds, weeks::Union{Nothing,Integer}=_weeks,
canonicalize::Bool = false)

y::Integer = something(years, 0)
m::Integer = something(months, 0)
d::Integer = something(days, 0)
h::Integer = something(hours, 0)
min::Integer = something(minutes, 0)
s::Integer = something(seconds, 0)
ms::Integer = something(milliseconds, 0)
µs::Integer = something(microseconds, 0)
ns::Integer = something(nanoseconds, 0)
w::Integer = something(weeks, 0)
cp = sum((
Year(y), Month(m),
# 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(days), Hour(hours), Minute(minutes), Second(seconds), Millisecond(milliseconds), Microsecond(microseconds), Nanosecond(nanoseconds), Week(weeks))
Year(y), Month(m), Week(w), Day(d), Hour(h), Minute(min), Second(s), Millisecond(ms), Microsecond(µs), Nanosecond(ns))
)
# make sure the correct unit is used when value is 0
if isempty(cp.periods) && year_or_month_given
pytimedelta64(Month(0))
if isempty(cp.periods)
Units = (Second, Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond)
index::Integer = findlast(!isnothing, (0, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds));
pytimedelta64(Units[index](0))
else
pytimedelta64(cp)
pytimedelta64(cp; canonicalize)
end
end
function pytimedelta64(@nospecialize(x::T)) where T <: Period
index = findfirst(==(T), (Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, T))::Int
function pytimedelta64(@nospecialize(x::T); canonicalize::Bool = false) where T <: Period
canonicalize && return pytimedelta64(@__MODULE__().canonicalize(x))

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)
function pytimedelta64(x::CompoundPeriod; canonicalize::Bool = false)
canonicalize && (x = @__MODULE__().canonicalize(x))
isempty(x.periods) ? pytimedelta64(Second(0)) : sum(pytimedelta64.(x.periods))
end
function pytimedelta64(x::Integer)
pyimport("numpy").timedelta64(x)
end
export pytimedelta64

function pyconvert_rule_datetime64(::Type{DateTime}, x::Py)
Expand All @@ -91,7 +114,9 @@ function pyconvert_rule_timedelta64(::Type{CompoundPeriod}, x::Py)
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)
cp = CompoundPeriod(T(value * count))
CANONICALIZE_TIMEDELTA64[] && (cp = @__MODULE__().canonicalize(cp))
pyconvert_return(cp)
end

function pyconvert_rule_timedelta64(::Type{T}, x::Py) where T<:Period
Expand Down