Skip to content

Commit e9277ef

Browse files
author
hhaensel
committed
change order of args of pytimedelta; add docstrings for pytimedelta, pytimedelta64, pydatetime64
1 parent f66f526 commit e9277ef

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/Convert/numpy.jl

+62
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ const NUMPY_SIMPLE_TYPES = [
2929
("complex128", ComplexF64),
3030
]
3131

32+
"""
33+
pydatetime64([year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, weeks])
34+
pydatetime64(; [year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, weeks])
35+
36+
Create a `numpy.pydatetime64` object from the given time arguments.
37+
Arguments can be supplied either as keyword arguments or positional arguments in the above order.
38+
39+
Examples:
40+
```julia
41+
pydatetime64(2025, 1, 2, 3, 4, 5, 6, 7, 8)
42+
# Python: np.datetime64('2025-01-02T03:04:05.006007008')
43+
44+
pydatetime64(year = 2025, month = 5, day = 20, nanosecond = 1)
45+
# Python: np.datetime64('2025-05-20T00:00:00.000000001')
46+
```
47+
"""
3248
function pydatetime64(
3349
_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;
3450
year::Integer=_year, month::Integer=_month, day::Integer=_day, hour::Integer=_hour, minute::Integer=_minute, second::Integer=_second,
@@ -48,6 +64,30 @@ function pydatetime64(x::Union{Date, DateTime})
4864
end
4965
export pydatetime64
5066

67+
"""
68+
pytimedelta64([years, months, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, weeks]; canonicalize=false)
69+
pytimedelta64(; [years, months, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, weeks], canonicalize=false)
70+
71+
Create a `numpy.timedelta64` object from the given time arguments.
72+
Arguments can be supplied either as keyword arguments or positional arguments in the above order.
73+
`years` and `months` cannot be mixed with other units.
74+
75+
Parameters:
76+
- `canonicalize=false`: If `false`, the unit of the result will be the unit of the least unit that was specified,
77+
if `true`, the result will be converted to least possible unit, e.g. 60s will be converted to 1m.
78+
79+
Examples:
80+
```julia
81+
pytimedelta64(1, 2)
82+
# Python: np.timedelta64(14,'M')
83+
84+
pytimedelta64(years = 1, months = 12; canonicalize = true)
85+
# Python: np.timedelta64(2,'Y')
86+
87+
pytimedelta64(hours = 3, minutes = 60)
88+
# Python: np.timedelta64(240,'m')
89+
```
90+
"""
5191
function pytimedelta64(
5292
_years::Union{Nothing,Integer}=nothing, _months::Union{Nothing,Integer}=nothing,
5393
_days::Union{Nothing,Integer}=nothing, _hours::Union{Nothing,Integer}=nothing,
@@ -83,6 +123,11 @@ function pytimedelta64(
83123
pytimedelta64(cp; canonicalize)
84124
end
85125
end
126+
"""
127+
pytimedelta64(x::Union{Period, CompoundPeriod}; canonicalize=false)
128+
129+
Convert a Julia `Period` or `CompoundPeriod` to a `numpy.timedelta64` object.
130+
"""
86131
function pytimedelta64(@nospecialize(x::T); canonicalize::Bool = false) where T <: Period
87132
canonicalize && return pytimedelta64(@__MODULE__().canonicalize(x))
88133

@@ -94,6 +139,23 @@ function pytimedelta64(x::CompoundPeriod; canonicalize::Bool = false)
94139
canonicalize && (x = @__MODULE__().canonicalize(x))
95140
isempty(x.periods) ? pytimedelta64(Second(0)) : sum(pytimedelta64.(x.periods))
96141
end
142+
"""
143+
pytimedelta64(x::Integer)
144+
145+
Create a dimensionless `numpy.timedelta64` object. If added to another `numpy.timedelta64` object, it will be interpreted as being of the same unit.
146+
147+
Examples:
148+
```julia
149+
pytimedelta64(2)
150+
# Python: np.timedelta64(2)
151+
152+
pytimedelta64(Hour(1)) + pytimedelta64(2)
153+
# Python: np.timedelta64(3,'h')
154+
155+
pytimedelta64(2) + pytimedelta64(Hour(1)) + pytimedelta64(Minute(1))
156+
# Python: np.timedelta64(181,'m')
157+
```
158+
"""
97159
function pytimedelta64(x::Integer)
98160
pyimport("numpy").timedelta64(x)
99161
end

src/Core/builtins.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -1167,16 +1167,24 @@ end
11671167
pydatetime(x::Date) = pydatetime(year(x), month(x), day(x))
11681168
export pydatetime
11691169

1170+
"""
1171+
pytimedelta([days, hours, minutes, seconds, milliseconds, microseconds, weeks])
1172+
1173+
Construct a Python `timedelta` object. Arguments can be supplied either as keyword arguments or positional arguments in the above order.
1174+
1175+
Note that we chose a decreasing order of the positional arguments (except `week`, which goes last) other than the Python function `datetime.timedelta()`.
1176+
This way the functions `pytimedelta()` and `pytimedelta64()` have a similar signature.
1177+
"""
11701178
function pytimedelta(
1171-
_days::Int=0, _seconds::Int=0, _microseconds::Int=0, _milliseconds::Int=0, _minutes::Int=0, _hours::Int=0, _weeks::Int=0;
1172-
days::Int=_days, seconds::Int=_seconds, microseconds::Int=_microseconds, milliseconds::Int=_milliseconds, minutes::Int=_minutes, hours::Int=_hours, weeks::Int=_weeks
1179+
_days::Int=0, _hours::Int=0, _minutes::Int=0, _seconds::Int=0, _milliseconds::Int=0, _microseconds::Int=0, _weeks::Int=0;
1180+
days::Int=_days, hours::Int=_hours, minutes::Int=_minutes, seconds::Int=_seconds, milliseconds::Int=_milliseconds, microseconds::Int=_microseconds, weeks::Int=_weeks
11731181
)
11741182
pyimport("datetime").timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
11751183
end
11761184
function pytimedelta(@nospecialize(x::T)) where T <: Period
11771185
T <: Union{Week, Day, Hour, Minute, Second, Millisecond, Microsecond} ||
11781186
error("Unsupported Period type: ", "Year, Month and Nanosecond are not supported, consider using pytimedelta64 instead.")
1179-
args = T .== (Day, Second, Microsecond, Millisecond, Minute, Hour, Week)
1187+
args = T .== (Day, Hour, Minute, Second, Millisecond, Microsecond, Week)
11801188
pytimedelta(x.value .* args...)
11811189
end
11821190
function pytimedelta(x::CompoundPeriod)

0 commit comments

Comments
 (0)