# Mean of Period

**URL:** <https://discourse.julialang.org/t/mean-of-period/57951>\
**Category:** General Usage\
**Created:** [March 25, 2021, 4:37pm UTC](https://discourse.julialang.org/t/mean-of-period/57951 "2021-03-25T16:37:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [March 25, 2021, 4:37pm UTC](https://discourse.julialang.org/t/mean-of-period/57951/1 "2021-03-25T16:37:22Z")

</div>

So reading the documentation, if you take a difference of DateTime you get an Period outputs (year, minute etc) and these are supposed to behave like integers. But if I would like the average period time (ex `mean([Second(2), Second(2), Second(3)])`). I get an error. But you can easily take the mean of an integer vector. Is this a design flaw? I know there is the workaround of `mean(Dates.value.([Second(2), Second(2), Second(3)]))`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 25, 2021, 4:56pm UTC](https://discourse.julialang.org/t/mean-of-period/57951/2 "2021-03-25T16:56:53Z")

</div>

this is because you can’t have float number of `Second`.

```julia
julia> Second(2.33)
ERROR: InexactError: Int64(2.33)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 25, 2021, 5:05pm UTC](https://discourse.julialang.org/t/mean-of-period/57951/3 "2021-03-25T17:05:56Z")

</div>

What is happening there is that the return value of `mean` is of the same type of the elements of the input array or a type to which a promotion makes sense.

Since the mean can be fractional, the mean cannot be of type `Second` (although it works when the mean is an integer:

```julia
julia> d = [Second(2), Second(4)]
2-element Vector{Second}:
 2 seconds
 4 seconds

julia> mean(d)
3 seconds

```

That said, there is a inner function in `Statistics` which is `_mean` and receives a function that will allow you to promote “by hand” the result to float:

```julia
julia> Statistics._mean(Dates.value,[Second(1), Second(2)])
1.5

```

The problem, thus, is to decide to what that result should be promoted. A specific method of `mean` could be implemented for that if it turns out to be used, but the workarounds are fairly quick, so I am not sure if it would be worth the case.
