Skip to content

Commit 63312d7

Browse files
author
Christopher Rowley
committed
add support for datetime64 and timedelta64
1 parent d969fbc commit 63312d7

File tree

3 files changed

+19
-36
lines changed

3 files changed

+19
-36
lines changed

docs/src/juliacall-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ jl.Vector[jl.Int]()
203203
204204
Some Julia types can be converted to corresponding numpy dtypes like `numpy.dtype(jl.Int)`.
205205
Currently supports these primitive types: `Bool`, `IntXX`, `UIntXX`, `FloatXX`,
206-
`ComplexFXX`, `Ptr{Cvoid}`.
206+
`ComplexFXX`, `NumpyDates.InlineDateTime64{unit}` and `NumpyDates.InlineTimeDelta64{unit}`.
207207
`````
208208

209209
`````@customdoc

src/JlWrap/type.jl

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,17 @@ function pyjltype_getitem(self::Type, k_)
1212
end
1313

1414
function pyjltype_numpy_dtype(self::Type)
15+
typestr, descr = pytypestrdescr(self)
16+
if isempty(typestr)
17+
errset(pybuiltins.AttributeError, "__numpy_dtype__")
18+
return PyNULL
19+
end
1520
np = pyimport("numpy")
16-
if self === Bool
17-
return np.dtype(np.bool_)
18-
elseif self === Int8
19-
return np.dtype(np.int8)
20-
elseif self === Int16
21-
return np.dtype(np.int16)
22-
elseif self === Int32
23-
return np.dtype(np.int32)
24-
elseif self === Int64
25-
return np.dtype(np.int64)
26-
elseif self === UInt8
27-
return np.dtype(np.uint8)
28-
elseif self === UInt16
29-
return np.dtype(np.uint16)
30-
elseif self === UInt32
31-
return np.dtype(np.uint32)
32-
elseif self === UInt64
33-
return np.dtype(np.uint64)
34-
elseif self === Float16
35-
return np.dtype(np.float16)
36-
elseif self === Float32
37-
return np.dtype(np.float32)
38-
elseif self === Float64
39-
return np.dtype(np.float64)
40-
elseif self === ComplexF32
41-
return np.dtype(np.complex64)
42-
elseif self === ComplexF64
43-
return np.dtype(np.complex128)
44-
elseif self === Ptr{Cvoid}
45-
return np.dtype("P")
21+
if pyisnull(descr)
22+
return np.dtype(typestr)
23+
else
24+
return np.dtype(descr)
4625
end
47-
errset(pybuiltins.AttributeError, "__numpy_dtype__")
48-
return PyNULL
4926
end
5027

5128
pyjl_handle_error_type(::typeof(pyjltype_numpy_dtype), x, exc) = pybuiltins.AttributeError

test/JlWrap.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ end
473473
end
474474

475475
@testitem "type" setup = [Setup] begin
476+
using PythonCall.NumpyDates
476477
@testset "type" begin
477478
@test pyis(pytype(pyjl(Int)), PythonCall.pyjltypetype)
478479
end
@@ -499,17 +500,22 @@ end
499500
(Float64, "float64"),
500501
(ComplexF32, "complex64"),
501502
(ComplexF64, "complex128"),
502-
(Ptr{Cvoid}, "P"),
503+
(InlineDateTime64{SECONDS}, "datetime64[s]"),
504+
(InlineDateTime64{(SECONDS, 5)}, "datetime64[5s]"),
505+
(InlineDateTime64{NumpyDates.UNBOUND_UNITS}, "datetime64"),
506+
(InlineTimeDelta64{MINUTES}, "timedelta64[m]"),
507+
(InlineTimeDelta64{(SECONDS, 5)}, "timedelta64[5s]"),
508+
(InlineTimeDelta64{NumpyDates.UNBOUND_UNITS}, "timedelta64"),
503509
]
504510
@test pyeq(Bool, pygetattr(pyjl(t), "__numpy_dtype__"), np.dtype(d))
505511
@test pyeq(Bool, np.dtype(pyjl(t)), np.dtype(d))
506512
end
507513

508514
# unsupported cases
509515
@testset "$t -> AttributeError" for t in [
510-
ComplexF16,
511516
String,
512-
Tuple{},
517+
Vector{Int},
518+
Ptr{Cvoid},
513519
Ptr{Int},
514520
Ptr{PythonCall.C.PyPtr},
515521
]

0 commit comments

Comments
 (0)