# Quarters into integers

**URL:** <https://discourse.julialang.org/t/quarters-into-integers/72712>\
**Category:** New to Julia\
**Tags:** question, dates\
**Created:** [December 7, 2021, 2:43pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712 "2021-12-07T14:43:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linnea](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@Linnea](https://discourse.julialang.org/u/Linnea)\
**Post date:** [December 7, 2021, 2:43pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/1 "2021-12-07T14:43:28Z")

</div>

Hi, I am having issues with the date format of an imported time series from a csv file since they are reported as strings (“1977-Q1” and so on). I have convert them into quarters by Dates.jl and MonthlyDates.jl, but I need to convert them into Integers. How can I do it ? Thanks

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [December 7, 2021, 2:45pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/2 "2021-12-07T14:45:16Z")

</div>

What would you like “1977-Q1” to become?

---

<div class="post-metadata">

**Author:** ![Linnea](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@Linnea](https://discourse.julialang.org/u/Linnea)\
**Post date:** [December 7, 2021, 2:50pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/3 "2021-12-07T14:50:54Z")

</div>

I need quarters for each year between 1977 and 2020 and I need each date to be an integer (for example to able to implement a linear model like lm(@formula(y ~ t), df) )

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [December 7, 2021, 2:54pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/4 "2021-12-07T14:54:08Z")

</div>

In that case you don’t really need to “convert” your 1977-Q1 to an integer. Just call the first quarter in your sample 1, the next quarter is 2, and so on. If you only have a single time series, this is just making a new column with `1:T`.

`df.intQ = 1:T` or something to that effect

---

<div class="post-metadata">

**Author:** ![Linnea](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@Linnea](https://discourse.julialang.org/u/Linnea)\
**Post date:** [December 7, 2021, 8:28pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/5 "2021-12-07T20:28:53Z")

</div>

> [@tbeason](#):
>
> df.intQ = 1:T

Thanks you!!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 7, 2021, 8:38pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/6 "2021-12-07T20:38:59Z")

</div>

The easiest might be to generate a dict with all possible `year-quarter` strings mapped to an index:

```julia
const quarters = Dict{String,Int}()
let i = 0
    for year = 1977:2020, quarter = 1:4
        quarters["$year-Q$quarter"] = i += 1
    end
end

```

Then when you see a year-quarter string, you can just look it up in the dict and get the index for it.

---

<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:** [December 7, 2021, 9:11pm UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/7 "2021-12-07T21:11:56Z")

</div>

> [@Linnea](#):
>
> I have convert them into quarters by Dates.jl and MonthlyDates.jl, but I need to convert them into Integers. How can I do it ?

Using those two packages you could do:

```julia
Dates.value(Date(QuarterlyDate("1977-Q1")))

```

_ **NB:** the integers will jump by ~90 days between consecutive quarters_

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 8, 2021, 3:06am UTC](https://discourse.julialang.org/t/quarters-into-integers/72712/8 "2021-12-08T03:06:10Z")

</div>

```julia
julia> f(s) = 4*(parse(Int,(s[1:4]))-1971) + (parse(Int,(s[end]))-1)

julia> f("1971-Q1")
0

julia> f("1971-Q2")
1

julia> f("1971-Q3")
2

julia> f("1971-Q4")
3

julia> f("2021-Q3")
202

```

If you’re trying to fit quarter to models, floating point number are just as good and easier to interpret:

```julia
julia> g(s) = parse(Int,(s[1:4])) + parse(Int,(s[end])-1)/4
g (generic function with 1 method)

julia> g("1971-Q1")
1971.0

julia> g("1971-Q2")
1971.25

```
