Nixtla / utilsforecast

https://nixtlaverse.nixtla.io/utilsforecast
Apache License 2.0
40 stars 6 forks source link

[BUG] fill_gaps fails with quarterly frequency #48

Closed AzulGarza closed 9 months ago

AzulGarza commented 9 months ago

Passing Q-related frequencies to fill_gaps throws the following error:

image
from utilsforecast.data import generate_series
from utilsforecast.preprocessing import fill_gaps

freq = 'Q-DEC'
series = generate_series(
    10, 
    freq=freq, 
    equal_ends=True, 
    min_length=10, 
    max_length=30,
)
fill_gaps(series, freq=freq, start='global')
AzulGarza commented 9 months ago

A workaround:

When using start='global', we don't need the correct dates while filling the gaps. We can transform the dates to daily frequency, fill the gaps, and then retransform the dates to the required frequency:

import pandas as pd
from utilsforecast.data import generate_series
from utilsforecast.preprocessing import fill_gaps

freq = 'Q-DEC'
series = generate_series(
    10, 
    freq=freq, 
    equal_ends=True, 
    min_length=10, 
    max_length=30,
)
dates = sorted(series['ds'].unique())
dates_replacer = dict(zip(dates, pd.date_range(end='2021-01-01', periods=len(dates))))
series_dates_replaced = series.replace(dates_replacer)
series_dates_replaced = fill_gaps(series_dates_replaced, freq='D', start='global')
series_filled = series_dates_replaced.replace({v: k for k, v in dates_replacer.items()})
assert series['y'].sum() == series_filled['y'].sum()
jmoralez commented 9 months ago

Would freq='3M' work here?

AzulGarza commented 9 months ago

It works perfectly, thank you @jmoralez🎉

image