Search code examples
f#deedle

Subtracting dates with Deedle


I want to get the difference between two dates. Ideally to add this back into the frame as a new column. Have tried the code below but get "Overloaded subtraction operator"

let df = Frame.ReadCsv("someDateColumns.csv", hasHeaders=true, separators="|")
let s1:Series<int,DateTime> = df.GetColumn<DateTime>("StartDate")
let s2:Series<int,DateTime> = df.GetColumn<DateTime>("EndDate")
    
    
let s3  = s2 - s1

Solution

  • I agree this would be a nice addition to Deedle. If you were thinking of contributing, it should be relatively easy to add - it would be a matter of adding another overload to the list of supported overloads in the Series module, which currently has vector binary operations for various numerical types and strings.

    Without changing Deedle, the easiest option is to use the Series.zipInto operation, which aligns two series (no-op if they came from the same data frame) and applies an operation to matching elements:

    #r "nuget: Deedle"
    open Deedle
    open System
    
    let df = 
      Frame.ofRecords 
        [ {| Start=DateTime(2000,1,1); End=DateTime(2000,1,10) |}
          {| Start=DateTime(2000,12,1); End=DateTime(2001,1,1) |}]
    
    let s1 = df.GetColumn<DateTime>("Start")
    let s2 = df.GetColumn<DateTime>("End")
        
    let s3  = Series.zipInto (-) s2 s1