Quarters into integers

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

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

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) )

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

2 Likes

Thanks you!!

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

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.

4 Likes

Using those two packages you could do:

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

NB: the integers will jump by ~90 days between consecutive quarters

1 Like
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> 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
2 Likes