diff --git a/test/Convert.jl b/test/Convert.jl index 4f137459..becb06e8 100644 --- a/test/Convert.jl +++ b/test/Convert.jl @@ -305,6 +305,59 @@ end @test_throws Exception pyconvert(Second, td(microseconds = 1000)) end +@testitem "timedelta64" begin + using Dates + using CondaPkg + CondaPkg.add("pandas") + using DataFrames + + dt1 = pytimedelta(seconds = 1) + dt2 = pytimedelta64(seconds = 1) + @test pyeq(Bool, dt1, dt2) + + @test pyeq(Bool, pytimedelta64(seconds = 10), pyimport("numpy").timedelta64(10, "s")) + @test pyeq(Bool, pytimedelta64(years = 10), pyimport("numpy").timedelta64(10, "Y")) + @test_throws Exception pytimedelta64(years = 10, seconds = 1) + + @testset for x in [ + -1_000_000_000, + -1_000_000, + -1_000, + -1, + 0, + 1, + 1_000, + 1_000_000, + 1_000_000_000, + ], (Unit, unit) in [ + (Nanosecond, :nanoseconds), + (Microsecond, :microseconds), + (Millisecond, :milliseconds), + (Second, :seconds), + (Minute, :minutes), + (Hour, :hours), + (Day, :days), + (Week, :weeks), + (Month, :months), + (Year, :years), + ] + y = pyconvert(Unit, pytimedelta64(; [unit => x]...)) + @test y === Unit(x) + end + @test_throws Exception pyconvert(Second, td(microseconds = 1000)) + + jdf = DataFrame(x = [now() + Second(rand(1:1000)) for _ in 1:100], y = [Second(n) for n in 1:100]) + pdf = pytable(jdf) + @test ispy(pdf.y) + @test pyeq(Bool, pdf.y[0], pytimedelta64(seconds = 1)) + # automatic conversion from pytimedelta64 converts to Dates.CompoundPeriod + jdf2 = DataFrame(PyPandasDataFrame(pdf)) + @test eltype(jdf2.y) == Dates.CompoundPeriod + # convert y column back to Seconds + jdf2.y = convert.(Second, jdf2.y) + @test pyeq(Bool, jdf, jdf2) +end + @testitem "pyconvert_add_rule (#364)" begin id = string(rand(UInt128), base = 16) pyexec( diff --git a/test/Core.jl b/test/Core.jl index feadf855..e8375f00 100644 --- a/test/Core.jl +++ b/test/Core.jl @@ -676,6 +676,9 @@ end @testitem "datetime" begin using Dates + using CondaPkg + CondaPkg.add("numpy") + dt = pyimport("datetime") x1 = pydate(2001, 2, 3) @test pyisinstance(x1, dt.date) @@ -701,6 +704,34 @@ end x8 = pydatetime(2001, 2, 3, 4, 5, 6, 7) dx = pytimedelta(366, 3661, 1) pyeq(Bool, x8 - x6, dx) + + td = pyimport("datetime").timedelta + @testset for x in [ + -1_000_000_000, + -1_000_000, + -1_000, + -1, + 0, + 1, + 1_000, + 1_000_000, + 1_000_000_000, + ], (Unit, unit, pyunit, factor) in [ + (Microsecond, :microseconds, :microseconds, 1), + (Millisecond, :milliseconds, :milliseconds, 1), + (Second, :seconds, :seconds, 1), + (Minute, :minutes, :seconds, 60), + (Hour, :hours, :hours, 1), + (Day, :days, :days, 1), + (Week, :weeks, :days, 7) + ] + # for day and week units, skip large values due to overflow + unit in [:days, :weeks] && abs(x) > 1_000_000 && continue + y = pytimedelta(; [unit => x]...) + y2 = pytimedelta(Unit(x)) + @test pyeq(Bool, y, y2) + @test pyeq(Bool, y, td(; [pyunit => x * factor]...)) + end end @testitem "code" begin diff --git a/test/runtests.jl b/test/runtests.jl index b9e874db..0660c407 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,3 +1,3 @@ using TestItemRunner - +using DataFrames @run_package_tests