Skip to content

Commit

Permalink
add tests for pytimedelta, pytimedelta64 and conversion of pytimedelt…
Browse files Browse the repository at this point in the history
…a64 in DataFrames
  • Loading branch information
hhaensel committed Jan 20, 2025
1 parent 8f28567 commit 46efe53
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
53 changes: 53 additions & 0 deletions test/Convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions test/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using TestItemRunner

using DataFrames
@run_package_tests

0 comments on commit 46efe53

Please sign in to comment.