Skip to content

Commit c8c411a

Browse files
author
Christopher Doris
committed
refactor(numpydates): simplify accessors and add inline datetime/timedelta support
- Simplified accessor functions in DateTime64, InlineDateTime64, InlineTimeDelta64, and TimeDelta64 by removing unnecessary function wrappers - Added support for NumpyDates.InlineDateTime64 and InlineTimeDelta64 in pyjlarray_isarrayabletype and pytypestrdescr - Refactored pytypestrdescr to use if-elseif structure for better readability - Updated PyArray.jl to handle unbound units in type conversion This enhances numpy date type integration while cleaning up code structure.
1 parent 4f3195a commit c8c411a

File tree

7 files changed

+52
-41
lines changed

7 files changed

+52
-41
lines changed

src/JlWrap/JlWrap.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module JlWrap
77

88
using ..PythonCall
99
using ..Utils
10+
using ..NumpyDates: NumpyDates
1011
using ..C
1112
using ..Core
1213
using ..Convert

src/JlWrap/array.jl

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ pyjlarray_isarrayabletype(::Type{T}) where {T} = T in (
223223
Complex{Float32},
224224
Complex{Float64},
225225
)
226+
pyjlarray_isarrayabletype(::Type{NumpyDates.InlineDateTime64{U}}) where {U} = true
227+
pyjlarray_isarrayabletype(::Type{NumpyDates.InlineTimeDelta64{U}}) where {U} = true
226228
pyjlarray_isarrayabletype(::Type{T}) where {T<:Tuple} =
227229
isconcretetype(T) &&
228230
Base.allocatedinline(T) &&
@@ -235,22 +237,44 @@ const PYTYPESTRDESCR = IdDict{Type,Tuple{String,Py}}()
235237
pytypestrdescr(::Type{T}) where {T} =
236238
get!(PYTYPESTRDESCR, T) do
237239
c = Utils.islittleendian() ? '<' : '>'
238-
T == Bool ? ("$(c)b$(sizeof(Bool))", PyNULL) :
239-
T == Int8 ? ("$(c)i1", PyNULL) :
240-
T == UInt8 ? ("$(c)u1", PyNULL) :
241-
T == Int16 ? ("$(c)i2", PyNULL) :
242-
T == UInt16 ? ("$(c)u2", PyNULL) :
243-
T == Int32 ? ("$(c)i4", PyNULL) :
244-
T == UInt32 ? ("$(c)u4", PyNULL) :
245-
T == Int64 ? ("$(c)i8", PyNULL) :
246-
T == UInt64 ? ("$(c)u8", PyNULL) :
247-
T == Float16 ? ("$(c)f2", PyNULL) :
248-
T == Float32 ? ("$(c)f4", PyNULL) :
249-
T == Float64 ? ("$(c)f8", PyNULL) :
250-
T == Complex{Float16} ? ("$(c)c4", PyNULL) :
251-
T == Complex{Float32} ? ("$(c)c8", PyNULL) :
252-
T == Complex{Float64} ? ("$(c)c16", PyNULL) :
253-
if isstructtype(T) && isconcretetype(T) && Base.allocatedinline(T)
240+
if T == Bool
241+
("$(c)b$(sizeof(Bool))", PyNULL)
242+
elseif T == Int8
243+
("$(c)i1", PyNULL)
244+
elseif T == UInt8
245+
("$(c)u1", PyNULL)
246+
elseif T == Int16
247+
("$(c)i2", PyNULL)
248+
elseif T == UInt16
249+
("$(c)u2", PyNULL)
250+
elseif T == Int32
251+
("$(c)i4", PyNULL)
252+
elseif T == UInt32
253+
("$(c)u4", PyNULL)
254+
elseif T == Int64
255+
("$(c)i8", PyNULL)
256+
elseif T == UInt64
257+
("$(c)u8", PyNULL)
258+
elseif T == Float16
259+
("$(c)f2", PyNULL)
260+
elseif T == Float32
261+
("$(c)f4", PyNULL)
262+
elseif T == Float64
263+
("$(c)f8", PyNULL)
264+
elseif T == Complex{Float16}
265+
("$(c)c4", PyNULL)
266+
elseif T == Complex{Float32}
267+
("$(c)c8", PyNULL)
268+
elseif T == Complex{Float64}
269+
("$(c)c16", PyNULL)
270+
elseif T <: Union{NumpyDates.InlineDateTime64,NumpyDates.InlineTimeDelta64}
271+
u, m = NumpyDates.unitpair(T)
272+
tc = T <: NumpyDates.InlineDateTime64 ? 'M' : 'm'
273+
us =
274+
u == NumpyDates.UNBOUND_UNITS ? "" :
275+
m == 1 ? "[$(Symbol(u))]" : "[$(m)$(Symbol(u))]"
276+
("$(c)$(tc)8$(us)", PyNULL)
277+
elseif isstructtype(T) && isconcretetype(T) && Base.allocatedinline(T)
254278
n = fieldcount(T)
255279
flds = []
256280
for i = 1:n

src/NumpyDates/DateTime64.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ end
2727

2828
# accessors
2929

30-
function Dates.value(d::DateTime64)
31-
d.value
32-
end
33-
34-
function unitpair(d::DateTime64)
35-
d.unit
36-
end
30+
Dates.value(d::DateTime64) = d.value
3731

32+
unitpair(d::DateTime64) = d.unit
3833

3934
# constructors
4035

src/NumpyDates/InlineDateTime64.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ end
2929

3030
# accessors
3131

32-
function Dates.value(d::InlineDateTime64)
33-
d.value
34-
end
32+
Dates.value(d::InlineDateTime64) = d.value
3533

36-
function unitpair(::InlineDateTime64{U}) where {U}
37-
unitpair(U)
38-
end
34+
unitpair(::InlineDateTime64{U}) where {U} = unitpair(U)
35+
unitpair(::Type{InlineDateTime64{U}}) where {U} = unitpair(U)
3936

4037
# constructors
4138

src/NumpyDates/InlineTimeDelta64.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ end
2424

2525
# accessors
2626

27-
function Dates.value(d::InlineTimeDelta64)
28-
d.value
29-
end
27+
Dates.value(d::InlineTimeDelta64) = d.value
3028

31-
function unitpair(::InlineTimeDelta64{U}) where {U}
32-
unitpair(U)
33-
end
29+
unitpair(::InlineTimeDelta64{U}) where {U} = unitpair(U)
30+
unitpair(::Type{InlineTimeDelta64{U}}) where {U} = unitpair(U)
3431

3532
# constructors
3633

src/NumpyDates/TimeDelta64.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ end
2323

2424
# accessors
2525

26-
function Dates.value(d::TimeDelta64)
27-
d.value
28-
end
26+
Dates.value(d::TimeDelta64) = d.value
2927

30-
function unitpair(d::TimeDelta64)
31-
d.unit
32-
end
28+
unitpair(d::TimeDelta64) = d.unit
3329

3430
# constructors
3531

src/Wrap/PyArray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ pyarray_typestrdescr_to_type(ts::String, descr::Py) = begin
278278
s = m[3] === nothing ? 1 : parse(Int, m[3])
279279
us = m[4] === nothing ? "" : m[4]
280280
u =
281+
us == "" ? NumpyDates.UNBOUND_UNITS :
281282
us == "Y" ? NumpyDates.YEARS :
282283
us == "M" ? NumpyDates.MONTHS :
283284
us == "W" ? NumpyDates.WEEKS :

0 commit comments

Comments
 (0)