@@ -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+ """
3248function 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})
4864end
4965export 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+ """
5191function 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
85125end
126+ """
127+ pytimedelta64(x::Union{Period, CompoundPeriod}; canonicalize=false)
128+
129+ Convert a Julia `Period` or `CompoundPeriod` to a `numpy.timedelta64` object.
130+ """
86131function 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))
96141end
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+ """
97159function pytimedelta64 (x:: Integer )
98160 pyimport (" numpy" ). timedelta64 (x)
99161end
0 commit comments