Skip to content
Permalink
Browse files
add ArrowTypes.default methods and tests for dates (#86)
  • Loading branch information
ericphanson committed Dec 24, 2020
1 parent 9932c1f commit f771c7305417356493ad76cbf0823061b4bd7572
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
@@ -52,6 +52,12 @@ function arrowvector(x, i, nl, fi, de, ded, meta; dictencoding::Bool=false, dict
return arrowvector(S, x, i, nl, fi, de, ded, meta; dictencode=dictencode, kw...)
end

# defaults for Dates types
ArrowTypes.default(::Type{Dates.Date}) = Dates.Date(1,1,1)
ArrowTypes.default(::Type{Dates.Time}) = Dates.Time(1,1,1)
ArrowTypes.default(::Type{Dates.DateTime}) = Dates.DateTime(1,1,1,1,1,1)
ArrowTypes.default(::Type{TimeZones.ZonedDateTime}) = TimeZones.ZonedDateTime(1,1,1,1,1,1,TimeZones.tz"UTC")

# conversions to arrow types
arrowvector(::Type{Dates.Date}, x, i, nl, fi, de, ded, meta; kw...) =
arrowvector(converter(DATE, x), i, nl, fi, de, ded, meta; kw...)
@@ -0,0 +1,45 @@
import Dates
import TimeZones

struct WrappedDate
x::Dates.Date
end
Arrow.ArrowTypes.registertype!(WrappedDate, WrappedDate)

struct WrappedTime
x::Dates.Time
end
Arrow.ArrowTypes.registertype!(WrappedTime, WrappedTime)

struct WrappedDateTime
x::Dates.DateTime
end
Arrow.ArrowTypes.registertype!(WrappedDateTime, WrappedDateTime)

struct WrappedZonedDateTime
x::TimeZones.ZonedDateTime
end
Arrow.ArrowTypes.registertype!(WrappedZonedDateTime, WrappedZonedDateTime)


@testset "Date and time wrappers with missing" begin
for T in (WrappedDate, WrappedTime, WrappedDateTime, WrappedZonedDateTime)
if T == WrappedZonedDateTime
time = T(Dates.now(TimeZones.tz"UTC"))
else
time = T(Dates.now())
end
table = (; x = [missing, missing, time, missing, time])
io = IOBuffer()
Arrow.write(io, table)
seekstart(io)
tbl = Arrow.Table(io)
@test isequal(collect(tbl.x), table.x)
end
end

@testset "`default(T) isa T`" begin
for T in (Dates.Date, Dates.Time, Dates.DateTime, TimeZones.ZonedDateTime, Dates.Nanosecond, Dates.Millisecond, Dates.Second, Dates.Day, Dates.Month, Dates.Year)
@test Arrow.ArrowTypes.default(T) isa T
end
end
@@ -18,6 +18,7 @@ using Test, Arrow, Tables, Dates, PooledArrays, TimeZones

include(joinpath(dirname(pathof(Arrow)), "../test/testtables.jl"))
include(joinpath(dirname(pathof(Arrow)), "../test/integrationtest.jl"))
include(joinpath(dirname(pathof(Arrow)), "../test/dates.jl"))

struct CustomStruct
x::Int

0 comments on commit f771c73

Please sign in to comment.