# Can't do statistics with Dates?

**URL:** https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020
**Category:** General Usage
**Created:** [January 27, 2021, 12:23am UTC](https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020 "2021-01-27T00:23:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [January 27, 2021, 12:23am UTC](https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020/1 "2021-01-27T00:23:07Z")

</div>

```julia
julia> d = diff(collect(Dates.Date(1980,1,1):Dates.Month(3):Dates.Date(2019,1,1)))
156-element Vector{Day}:
 91 days
 91 days
 92 days
 92 days
...

```

```julia
julia> mean(d)
ERROR: InexactError: Int64(91.31410256410257)

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [January 27, 2021, 12:45am UTC](https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020/2 "2021-01-27T00:45:00Z")

</div>

Dates uses an exact integer representation, which is helpful in a lot of cases, but sometimes a floating point representation is more useful. I’ve been using functions like

```julia
using Unitful, Dates

"""
     seconds(s) -> Unitful.Quantity

 Converts a `Dates.TimePeriod` or Unitful time quantity to seconds.
 """
 seconds

 seconds(s::Unitful.Quantity) = uconvert(u"s", s)
 seconds(s::TimePeriod) = seconds(Nanosecond(s))
 seconds(s::Nanosecond) = Dates.value(s)*1e-9 * u"s"

```

as an easy way to convert to one. (With a loop to then also similarly define minutes, hours, days, etc.). It’s been working fine for me, except for that `StatsBase.summarystats` is too narrowly typed to handle Unitful quantities.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [January 27, 2021, 12:56am UTC](https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020/3 "2021-01-27T00:56:07Z")

</div>

Perhaps

```julia
julia> mean(getfield.(d,:value))
91.31410256410257

```

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [January 27, 2021, 1:05am UTC](https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020/4 "2021-01-27T01:05:45Z")

</div>

Thanks. That will have to do it but won’t stand favorable next to the Matlab code I’m replicating

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/a/cacd77acc5e43627f74454e10af1bfa618ca0339.png)

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [January 27, 2021, 1:35am UTC](https://discourse.julialang.org/t/cant-do-statistics-with-dates/54020/5 "2021-01-27T01:35:28Z")

</div>

You only need to convert the Days to Int64s once when assigning to durs. Afterwards you statistical function calls look about the same.
