Skip to content

Commit 46efe53

Browse files
author
hhaensel
committed
add tests for pytimedelta, pytimedelta64 and conversion of pytimedelta64 in DataFrames
1 parent 8f28567 commit 46efe53

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

test/Convert.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,59 @@ end
305305
@test_throws Exception pyconvert(Second, td(microseconds = 1000))
306306
end
307307

308+
@testitem "timedelta64" begin
309+
using Dates
310+
using CondaPkg
311+
CondaPkg.add("pandas")
312+
using DataFrames
313+
314+
dt1 = pytimedelta(seconds = 1)
315+
dt2 = pytimedelta64(seconds = 1)
316+
@test pyeq(Bool, dt1, dt2)
317+
318+
@test pyeq(Bool, pytimedelta64(seconds = 10), pyimport("numpy").timedelta64(10, "s"))
319+
@test pyeq(Bool, pytimedelta64(years = 10), pyimport("numpy").timedelta64(10, "Y"))
320+
@test_throws Exception pytimedelta64(years = 10, seconds = 1)
321+
322+
@testset for x in [
323+
-1_000_000_000,
324+
-1_000_000,
325+
-1_000,
326+
-1,
327+
0,
328+
1,
329+
1_000,
330+
1_000_000,
331+
1_000_000_000,
332+
], (Unit, unit) in [
333+
(Nanosecond, :nanoseconds),
334+
(Microsecond, :microseconds),
335+
(Millisecond, :milliseconds),
336+
(Second, :seconds),
337+
(Minute, :minutes),
338+
(Hour, :hours),
339+
(Day, :days),
340+
(Week, :weeks),
341+
(Month, :months),
342+
(Year, :years),
343+
]
344+
y = pyconvert(Unit, pytimedelta64(; [unit => x]...))
345+
@test y === Unit(x)
346+
end
347+
@test_throws Exception pyconvert(Second, td(microseconds = 1000))
348+
349+
jdf = DataFrame(x = [now() + Second(rand(1:1000)) for _ in 1:100], y = [Second(n) for n in 1:100])
350+
pdf = pytable(jdf)
351+
@test ispy(pdf.y)
352+
@test pyeq(Bool, pdf.y[0], pytimedelta64(seconds = 1))
353+
# automatic conversion from pytimedelta64 converts to Dates.CompoundPeriod
354+
jdf2 = DataFrame(PyPandasDataFrame(pdf))
355+
@test eltype(jdf2.y) == Dates.CompoundPeriod
356+
# convert y column back to Seconds
357+
jdf2.y = convert.(Second, jdf2.y)
358+
@test pyeq(Bool, jdf, jdf2)
359+
end
360+
308361
@testitem "pyconvert_add_rule (#364)" begin
309362
id = string(rand(UInt128), base = 16)
310363
pyexec(

test/Core.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ end
676676

677677
@testitem "datetime" begin
678678
using Dates
679+
using CondaPkg
680+
CondaPkg.add("numpy")
681+
679682
dt = pyimport("datetime")
680683
x1 = pydate(2001, 2, 3)
681684
@test pyisinstance(x1, dt.date)
@@ -701,6 +704,34 @@ end
701704
x8 = pydatetime(2001, 2, 3, 4, 5, 6, 7)
702705
dx = pytimedelta(366, 3661, 1)
703706
pyeq(Bool, x8 - x6, dx)
707+
708+
td = pyimport("datetime").timedelta
709+
@testset for x in [
710+
-1_000_000_000,
711+
-1_000_000,
712+
-1_000,
713+
-1,
714+
0,
715+
1,
716+
1_000,
717+
1_000_000,
718+
1_000_000_000,
719+
], (Unit, unit, pyunit, factor) in [
720+
(Microsecond, :microseconds, :microseconds, 1),
721+
(Millisecond, :milliseconds, :milliseconds, 1),
722+
(Second, :seconds, :seconds, 1),
723+
(Minute, :minutes, :seconds, 60),
724+
(Hour, :hours, :hours, 1),
725+
(Day, :days, :days, 1),
726+
(Week, :weeks, :days, 7)
727+
]
728+
# for day and week units, skip large values due to overflow
729+
unit in [:days, :weeks] && abs(x) > 1_000_000 && continue
730+
y = pytimedelta(; [unit => x]...)
731+
y2 = pytimedelta(Unit(x))
732+
@test pyeq(Bool, y, y2)
733+
@test pyeq(Bool, y, td(; [pyunit => x * factor]...))
734+
end
704735
end
705736

706737
@testitem "code" begin

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
using TestItemRunner
2-
2+
using DataFrames
33
@run_package_tests

0 commit comments

Comments
 (0)