Skip to content

Commit d36c113

Browse files
committed
fix pytimdelta(years/months=0), add pydatetime64(::Union{Date, DateTime}), remove pydatetime64(::CompoundPeriod)
1 parent 46efe53 commit d36c113

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/Convert/numpy.jl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,29 @@ function pydatetime64(@nospecialize(x::T)) where T <: Period
4141
args = map(Base.Fix1(isa, x), (Day, Second, Millisecond, Microsecond, Minute, Hour, Week))
4242
pydatetime64(map(Base.Fix1(*, x.value), args)...)
4343
end
44-
function pydatetime64(x::CompoundPeriod)
45-
x = canonicalize(x)
46-
isempty(x.periods) ? pydatetime64(Second(0)) : sum(pydatetime64, x.periods)
44+
function pydatetime64(x::Union{Date, DateTime})
45+
pyimport("numpy").datetime64("$x")
4746
end
4847
export pydatetime64
4948

5049
function pytimedelta64(
51-
_years::Integer=0, _months::Integer=0, _days::Integer=0, _hours::Integer=0, _minutes::Integer=0, _seconds::Integer=0, _milliseconds::Integer=0, _microseconds::Integer=0, _nanoseconds::Integer=0, _weeks::Integer=0;
52-
years::Integer=_years, months::Integer=_months, days::Integer=_days, hours::Integer=_hours, minutes::Integer=_minutes, seconds::Integer=_seconds, microseconds::Integer=_microseconds, milliseconds::Integer=_milliseconds, nanoseconds::Integer=_nanoseconds, weeks::Integer=_weeks)
53-
pytimedelta64(sum((
54-
Year(years), Month(months),
50+
_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;
51+
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)
52+
year_or_month_given = (years !== nothing || months !== nothing)
53+
y::Integer = something(years, 0)
54+
m::Integer = something(months, 0)
55+
cp = sum((
56+
Year(y), Month(m),
5557
# you cannot mix year or month with any of the below units in python
5658
# in case of wrong usage a descriptive error message will by thrown by the underlying python function
5759
Day(days), Hour(hours), Minute(minutes), Second(seconds), Millisecond(milliseconds), Microsecond(microseconds), Nanosecond(nanoseconds), Week(weeks))
58-
))
60+
)
61+
# make sure the correct unit is used when value is 0
62+
if isempty(cp.periods) && year_or_month_given
63+
pytimedelta64(Month(0))
64+
else
65+
pytimedelta64(cp)
66+
end
5967
end
6068
function pytimedelta64(@nospecialize(x::T)) where T <: Period
6169
index = findfirst(==(T), (Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, T))::Int

0 commit comments

Comments
 (0)