# Data frame sum of a Time column

**URL:** <https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738>\
**Category:** New to Julia\
**Tags:** question, dates, dataframes\
**Created:** [October 14, 2022, 3:05pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738 "2022-10-14T15:05:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 14, 2022, 3:05pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/1 "2022-10-14T15:05:11Z")

</div>

Simple (I hope): how to sum the Time column (d) of 15:52:31 + 15:52:31 + 15:52:31 giving 47:34:33  
 ![Screenshot from 2022-10-14 16-02-50](https://global.discourse-cdn.com/julialang/original/3X/b/4/b456203500b96ec0759300877b54c53182fd2ac9.png)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 14, 2022, 3:16pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/2 "2022-10-14T15:16:49Z")

</div>

Your current type is `Time`, which is a time in a day. You can’t add two times in a day together.

What you want is a `Period`

```julia
julia> t1 = Dates.CompoundPeriod(Hour(15), Minute(52), Second(31));

julia> t2 = Dates.CompoundPeriod(Hour(2), Minute(25), Second(10));

julia> t1 + t2
17 hours, 77 minutes, 41 seconds

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 14, 2022, 7:18pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/3 "2022-10-14T19:18:10Z")

</div>

> [@StuartRL](#):
>
> 15:52:31 + 15:52:31 + 15:52:31 giving 47:34:33

**NB:** the sum should be `47:37:33`

Also, the addition of CompoundPeriods does not seem sufficient:

```julia
t1 = Dates.CompoundPeriod(Hour(15), Minute(52), Second(31))
t1 + t1 + t1
45 hours, 156 minutes, 93 seconds

```

One attempt here below, based on [this Rosetta code](https://rosettacode.org/wiki/Convert_seconds_to_compound_duration#Julia):

```julia
using Dates, DataFrames

function HHMMSS_duration(sec)
    t = Int[]
    for dm in (60, 60)
        sec, m = divrem(sec, dm)
        pushfirst!(t, m)
    end
    pushfirst!(t, sec)
    return Dates.CompoundPeriod(Hour(t[1]), Minute(t[2]), Second(t[3]))
end

df = DataFrame(a=1:3, d=Dates.Time.(["15:52:31","15:52:31","15:52:31"], "H:M:S"))

s = cumsum(Dates.value.(df.d))/1e9
df.cumsum = HHMMSS_duration.(s)

julia> df
 Row │ a d cumsum
     │ Int64 Time Compound…
─────┼───────────────────────────────────────────────────
   1 │ 1 15:52:31 15 hours, 52 minutes, 31 seconds
   2 │ 2 15:52:31 31 hours, 45 minutes, 2 seconds
   3 │ 3 15:52:31 47 hours, 37 minutes, 33 seconds

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 14, 2022, 11:31pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/4 "2022-10-14T23:31:11Z")

</div>

`canonicalize` might help. It does what its name implies or what this example shows:

```julia
julia> cp = Dates.CompoundPeriod(Hour(2),Minute(100),Second(5))
2 hours, 100 minutes, 5 seconds

julia> canonicalize(cp)
3 hours, 40 minutes, 5 seconds

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 14, 2022, 11:35pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/5 "2022-10-14T23:35:53Z")

</div>

> [@Dan](#):
>
> `canonicalize` might help.

If we take the OP example, it produces:

```julia
t1 = Dates.CompoundPeriod(Hour(15), Minute(52), Second(31))
canonicalize(t1 + t1 + t1)

1 day, 23 hours, 37 minutes, 33 seconds

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 15, 2022, 2:42am UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/6 "2022-10-15T02:42:07Z")

</div>

Adding times of the day makes no sense. `(15:00)*3=45:00` assumes that there is something special about midnight, but it just depends on which offset you choose.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 15, 2022, 5:31am UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/7 "2022-10-15T05:31:02Z")

</div>

Of course it doesn’t, and Peter already mentioned it above: a different type should be used. Meanwhile, if we reinterpret them as durations, how can we proceed?

---

<div class="post-metadata">

**Author:** ![chiraganand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiraganand/32/32787_2.png) [@chiraganand](https://discourse.julialang.org/u/chiraganand)\
**Post date:** [October 15, 2022, 9:16am UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/8 "2022-10-15T09:16:06Z")

</div>

`Time` is essentially nanoseconds so this should work?

```julia
julia> t1 = Time(15, 52, 31)
julia> t2 = Time(15, 52, 31)
julia> t3 = Time(15, 52, 31)

julia> canonicalize(t1.instant + t2.instant + t3.instant)
1 day, 23 hours, 37 minutes, 33 seconds

```

---

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 18, 2022, 3:01pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/9 "2022-10-18T15:01:07Z")

</div>

Thank you all and so I did two things with the second function feeling rather clumsy…(but I’m learning stuff). Q. How do you pass a data column name by reference to a function i.e. total\_time(accepted.Time).

“Total a dataframe Time column in Weeks, Days, Hours, Minutes and Seconds”  
function total\_time\_formal(xxx)  
t = Dates.CompoundPeriod(Hour(0), Minute(0), Second(0))  
for i in 1:nrow(xxx)  
t += Dates.CompoundPeriod(xxx.Time[i])  
end  
return canonicalize(t)  
end

“Total a dataframe Time column for HH:MM:SS i.e. hundreds of hours:minutes:seconds”  
function total\_time()  
t1 = t2 = t3 = 0.0 # hours, minutes, seconds  
for i in 1:nrow(accepted)  
t1 += Dates.hour(accepted.Time[i])  
t2 += Dates.minute(accepted.Time[i])  
t3 += Dates.second(accepted.Time[i])  
end  
t2 += modf(t3 / 60)[2] # whole seconds to minutes  
t3 = modf(t3 / 60)[1] \* 60 # residual seconds  
t1 += modf(t2 / 60)[2] # whole minutes to hours  
t2 = modf(t2 / 60)[1] \* 60 # residual minutes  
return t1, t2, t3  
end

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 18, 2022, 3:39pm UTC](https://discourse.julialang.org/t/data-frame-sum-of-a-time-column/88738/10 "2022-10-18T15:39:21Z")

</div>

Another way to convert higher periods (days, weeks) to hours:

```julia
julia> hourify(t) = begin
  tt = canonicalize(t) ; 
  pushfirst!(tt.periods, sum(Hour.(splice!(tt.periods,1:findlast(in([Hour,Day,Week]), typeof.(tt.periods))))))
end
hourify (generic function with 1 method)

julia> hourify(Second(123121414))
3-element Vector{Period}:
 34200 hours
 23 minutes
 34 seconds

```

The positive about this method is keeping the calculations hidden at the Dates module. Perhaps the technique willl be of sum use.
