# Best way to infer precision for periods (Date)

**URL:** https://discourse.julialang.org/t/best-way-to-infer-precision-for-periods-date/7165
**Category:** General Usage
**Tags:** dates, development
**Created:** [November 19, 2017, 6:27am UTC](https://discourse.julialang.org/t/best-way-to-infer-precision-for-periods-date/7165 "2017-11-19T06:27:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [November 19, 2017, 6:27am UTC](https://discourse.julialang.org/t/best-way-to-infer-precision-for-periods-date/7165/1 "2017-11-19T06:27:53Z")

</div>

Given an `AbstractVector{T} where T <: Dates.AbstractTime` what would be a good way to infer the precision of the dates/times?

For example, consider

```julia
obj = Date("2000"):Dates.Year(1):Date("2009")

```

when first differencing the data one gets,

```julia
distance = diff(obj)
9-element Vector{Base.Dates.Day}:
 366 days
 365 days
 365 days
 365 days
 366 days
 365 days
 365 days
 365 days
 366 days

```

The ideal solution would be to get `Dates.Year(1)`. My current approach is the following,

```julia
minimum(filter(elem -> elem > zero(Dates.Nanosecond),
        first.(getfield.(Dates.canonicalize.(
        Dates.CompoundPeriod.(diff(obj))), :periods))))
52 weeks

```

Hence I am able to get rid of the noise (being off by 1 - 2 days) and get a good measure of the frequency. For trimesters I get around `12 weeks` and for biannual frequency `25 weeks`. Is there a better method for inferring frequency / precision?

@quinnj was there development of a related feature?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 19, 2017, 7:54am UTC](https://discourse.julialang.org/t/best-way-to-infer-precision-for-periods-date/7165/2 "2017-11-19T07:54:36Z")

</div>

I think the question is not well-defined, since `Date(2000)` is equivalent to `Date(2000, 1, 1)`. Given the object, Julia cannot infer that the month and day were filled in as defaults, so the precision is a day as for other dates.

The best solution may be using `floor`, `ceil`, or `round`, depending on what you want.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [November 19, 2017, 4:59pm UTC](https://discourse.julialang.org/t/best-way-to-infer-precision-for-periods-date/7165/3 "2017-11-19T16:59:01Z")

</div>

Ended up implementing the following solution.

```julia
function frequency(obj::AbstractVector{T}) where T <: Dates.Date
    FirstDifference = diff(obj)
    FormalFirstDifference = Dates.canonicalize.(Dates.CompoundPeriod.(FirstDifference))
    if all(length.(getfield.(FormalFirstDifference, :periods)) .== 1)
        output = Dates.CompoundPeriod(minimum(FirstDifference))
        output = Dates.canonicalize(output)
    else
        output = diff(map(ymd -> [12 * ymd[1] + ymd[2], ymd[3]], Dates.yearmonthday.(obj)))
        if all(getindex.(output, 2) .== 0)
            output = minimum(reduce(vcat, first.(output)))
            output = Dates.canonicalize(Dates.CompoundPeriod(Dates.Month(output)))
        else
            output = Dates.CompoundPeriod(minimum(FirstDifference))
        end
    end
    return output
end

```
