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

Support DateTime math #182

Open
omus opened this issue Apr 26, 2016 · 1 comment
Open

Support DateTime math #182

omus opened this issue Apr 26, 2016 · 1 comment

Comments

@omus
Copy link
Contributor

omus commented Apr 26, 2016

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
@nalimilan
Copy link
Member

Yes, I had noticed this too at JuliaData/DataFrames.jl#937. Pull request welcome! Note that this should probably be Base.promote_op(@functorize(-), ..) for type stability in Julia 0.4 (see JuliaLang/Compat.jl#184).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants