Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.
This repository was archived by the owner on May 4, 2019. It is now read-only.

Support DateTime math #182

Open
Open
@omus

Description

@omus

Subtracting two DataArray{DateTime} arrays currently doesn't work:

julia> using DataFrames

julia> df = DataFrame()
0x0 DataFrames.DataFrame


julia> df[:A] = [DateTime(2015,1,1), DateTime(2015,1,2)]
2-element Array{DateTime,1}:
 2015-01-01T00:00:00
 2015-01-02T00:00:00

julia> df[:B] = [DateTime(2015,2,1), DateTime(2015,2,2)]
2-element Array{DateTime,1}:
 2015-02-01T00:00:00
 2015-02-02T00:00:00

julia> df[:A] - df[:B]
ERROR: MethodError: Cannot `convert` an object of type Base.Dates.Millisecond to an object of type DateTime
This may have arisen from a call to the constructor DateTime(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert(::Type{DateTime}, ::Date)
  convert{R<:Real}(::Type{DateTime}, ::R<:Real)
  convert{T}(::Type{T}, ::T)
  ...
 [inlined code] from ./dates/arithmetic.jl:9
 in -(::DataArrays.DataArray{DateTime,1}, ::DataArrays.DataArray{DateTime,1}) at /Users/omus/.julia/v0.5/DataArrays/src/operators.jl:332
 in eval(::Module, ::Any) at ./boot.jl:236

julia> Array{DateTime}(df[:A]) - Array{DateTime}(df[:B])
2-element Array{Base.Dates.Millisecond,1}:
 -2678400000 milliseconds
 -2678400000 milliseconds

From my brief investigation into the issue it appears that promote_type is used to determine the type of the produced DataArray. Wouldn't it make more sense to use promote_op for this?

julia> Base.promote_type(DateTime, DateTime)
DateTime

julia> Base.promote_op(-, DateTime, DateTime)
Base.Dates.Millisecond

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions