Home > Mobile >  How to construct a list containing values of missing time series years and quarters?
How to construct a list containing values of missing time series years and quarters?

Time:12-09

The data I have stops at 2020Q2 and the available data goes up to 2022Q3. So:

year_missing_end = 2022
quarter_missing_end = 3
num_quarter_missing = 8

How can I construct a list containing values of missing years and quarters? The result looks like this

missing_year_quarter_list = ['2020Q3', '2020Q4', '2021Q1', '2021Q2', '2021Q3', '2021Q4', '2022Q1', '2022Q2']

I try something like this but it doesn't work:

for x in range(0, num_quater_missing):
    print((x   quarter_missing_end)%4   1)

CodePudding user response:

Try:

year_missing_end = 2022
quarter_missing_end = 3
num_quarter_missing = 8


curr_quarter = quarter_missing_end - 1
while num_quarter_missing > 0:
    print(f"{year_missing_end}Q{curr_quarter}")

    curr_quarter -= 1
    num_quarter_missing -= 1

    if curr_quarter == 0:
        curr_quarter = 4
        year_missing_end -= 1

Prints:

2022Q2
2022Q1
2021Q4
2021Q3
2021Q2
2021Q1
2020Q4
2020Q3
  • Related