Skip to content

Commit b66d37d

Browse files
author
Christopher Doris
committed
test(numpydates): add tests for Date to DateTime64 unit mapping
Add NumpyDates tests verifying Date conversions to DateTime64 and InlineDateTime64 across units Y, M, D, h, m, s, ms, us, ns, comparing against expected int values. Covers: - DateTime64(Date, Symbol) - InlineDateTime64{U}(Date) - InlineDateTime64(Date, Symbol) and checks Dates.value and unitpair correctness. Add test/scripts/np_dates.py to generate NumPy-based ground-truth int64 values for selected dates.
1 parent 4ad1812 commit b66d37d

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

test/NumpyDates.jl

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
@testitem "Date -> DateTime64/InlineDateTime64" begin
2+
using Dates
3+
using PythonCall: NumpyDates
4+
5+
# Mapping from NumPy unit symbols to NumpyDates Unit constants
6+
const UNIT_CONST = Dict(
7+
:Y => NumpyDates.YEARS,
8+
:M => NumpyDates.MONTHS,
9+
:D => NumpyDates.DAYS,
10+
:h => NumpyDates.HOURS,
11+
:m => NumpyDates.MINUTES,
12+
:s => NumpyDates.SECONDS,
13+
:ms => NumpyDates.MILLISECONDS,
14+
:us => NumpyDates.MICROSECONDS,
15+
:ns => NumpyDates.NANOSECONDS,
16+
)
17+
18+
# Data generated by: uv run scripts/np_dates_all_units.py
19+
# Format: (Date, unit_symbol, numpy_int_value)
20+
cases = [
21+
(Date(1969, 12, 31), :Y, -1),
22+
(Date(1969, 12, 31), :M, -1),
23+
(Date(1969, 12, 31), :D, -1),
24+
(Date(1969, 12, 31), :h, -24),
25+
(Date(1969, 12, 31), :m, -1440),
26+
(Date(1969, 12, 31), :s, -86400),
27+
(Date(1969, 12, 31), :ms, -86400000),
28+
(Date(1969, 12, 31), :us, -86400000000),
29+
(Date(1969, 12, 31), :ns, -86400000000000),
30+
(Date(1970, 1, 1), :Y, 0),
31+
(Date(1970, 1, 1), :M, 0),
32+
(Date(1970, 1, 1), :D, 0),
33+
(Date(1970, 1, 1), :h, 0),
34+
(Date(1970, 1, 1), :m, 0),
35+
(Date(1970, 1, 1), :s, 0),
36+
(Date(1970, 1, 1), :ms, 0),
37+
(Date(1970, 1, 1), :us, 0),
38+
(Date(1970, 1, 1), :ns, 0),
39+
(Date(1970, 1, 2), :Y, 0),
40+
(Date(1970, 1, 2), :M, 0),
41+
(Date(1970, 1, 2), :D, 1),
42+
(Date(1970, 1, 2), :h, 24),
43+
(Date(1970, 1, 2), :m, 1440),
44+
(Date(1970, 1, 2), :s, 86400),
45+
(Date(1970, 1, 2), :ms, 86400000),
46+
(Date(1970, 1, 2), :us, 86400000000),
47+
(Date(1970, 1, 2), :ns, 86400000000000),
48+
(Date(1999, 12, 31), :Y, 29),
49+
(Date(1999, 12, 31), :M, 359),
50+
(Date(1999, 12, 31), :D, 10956),
51+
(Date(1999, 12, 31), :h, 262944),
52+
(Date(1999, 12, 31), :m, 15776640),
53+
(Date(1999, 12, 31), :s, 946598400),
54+
(Date(1999, 12, 31), :ms, 946598400000),
55+
(Date(1999, 12, 31), :us, 946598400000000),
56+
(Date(1999, 12, 31), :ns, 946598400000000000),
57+
(Date(2000, 2, 29), :Y, 30),
58+
(Date(2000, 2, 29), :M, 361),
59+
(Date(2000, 2, 29), :D, 11016),
60+
(Date(2000, 2, 29), :h, 264384),
61+
(Date(2000, 2, 29), :m, 15863040),
62+
(Date(2000, 2, 29), :s, 951782400),
63+
(Date(2000, 2, 29), :ms, 951782400000),
64+
(Date(2000, 2, 29), :us, 951782400000000),
65+
(Date(2000, 2, 29), :ns, 951782400000000000),
66+
(Date(1900, 1, 1), :Y, -70),
67+
(Date(1900, 1, 1), :M, -840),
68+
(Date(1900, 1, 1), :D, -25567),
69+
(Date(1900, 1, 1), :h, -613608),
70+
(Date(1900, 1, 1), :m, -36816480),
71+
(Date(1900, 1, 1), :s, -2208988800),
72+
(Date(1900, 1, 1), :ms, -2208988800000),
73+
(Date(1900, 1, 1), :us, -2208988800000000),
74+
(Date(1900, 1, 1), :ns, -2208988800000000000),
75+
(Date(2100, 1, 1), :Y, 130),
76+
(Date(2100, 1, 1), :M, 1560),
77+
(Date(2100, 1, 1), :D, 47482),
78+
(Date(2100, 1, 1), :h, 1139568),
79+
(Date(2100, 1, 1), :m, 68374080),
80+
(Date(2100, 1, 1), :s, 4102444800),
81+
(Date(2100, 1, 1), :ms, 4102444800000),
82+
(Date(2100, 1, 1), :us, 4102444800000000),
83+
(Date(2100, 1, 1), :ns, 4102444800000000000),
84+
]
85+
86+
for (d, usym, expected) in cases
87+
# 1) DateTime64 with UnitArg symbol
88+
dt64 = NumpyDates.DateTime64(d, usym)
89+
@test Dates.value(dt64) == expected
90+
91+
# 2) InlineDateTime64 with type parameter Unit constant
92+
Uconst = UNIT_CONST[usym]
93+
inline_typed = NumpyDates.InlineDateTime64{Uconst}(d)
94+
@test Dates.value(inline_typed) == expected
95+
96+
# 3) InlineDateTime64 with runtime UnitArg symbol
97+
inline_dyn = NumpyDates.InlineDateTime64(d, usym)
98+
@test Dates.value(inline_dyn) == expected
99+
@test NumpyDates.unitpair(inline_dyn) == NumpyDates.unitpair(usym)
100+
end
101+
end

test/scripts/np_dates.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
import numpy as np
3+
4+
dates = [
5+
"1969-12-31",
6+
"1970-01-01",
7+
"1970-01-02",
8+
"1999-12-31",
9+
"2000-02-29",
10+
"1900-01-01",
11+
"2100-01-01",
12+
]
13+
14+
# Note: skip 'W' to avoid week-anchor semantics; test uses floor(days/7) semantics.
15+
units = ["Y", "M", "D", "h", "m", "s", "ms", "us", "ns"]
16+
17+
# Output format: "<ISO_DATE> <UNIT> <INT_VALUE>"
18+
for s in dates:
19+
for u in units:
20+
d = np.datetime64(s, u)
21+
print(f"{s} {u} {int(d.astype('int64'))}")

0 commit comments

Comments
 (0)