Discussions

Ask a Question
Back to All

Struggling with Irregular TimeStamps Freq

Hey everyone,

I’m trying to forecast a time series using TimeGPT, but I’m running into an issue with the freq parameter. My dataset consists of stock market trading days, where I’ve already filtered out holidays using pandas_market_calendars (as per the method suggested in the Irregular Timestamps tutotial). However, TimeGPT doesn’t seem to infer the custom business frequency correctly. It is throwing this error due to non-inferrable frequency

"""
ValueError: Series contain missing or duplicate timestamps, or the timestamps do not match the provided frequency.
Please make sure that all series have a single observation from the first to the last timestamp and that the provided frequency matches the timestamps'.
You can refer to https://docs.nixtla.io/docs/tutorials-missing_values for an end to end example.

"""

I've attempted to pass a CustomBusinessDay offset to freq, but it still doesn’t work. How do I correctly set freq so that TimeGPT recognizes my dataset as having custom business days and forecasts accordingly?

Here’s my full code:

import pandas as pd  
import numpy as np  
from datetime import datetime  
from nixtla import NixtlaClient  
import pandas_market_calendars as mcal

nixtla_client = NixtlaClient(api_key='your_api_key')

end_date = datetime.strptime('2025-02-14', '%Y-%m-%d')  
dates = pd.date_range(end=end_date, periods=450, freq='B')

holidays = [  
    '2024-01-01',  # New Year's Day  
    # '2024-01-15',  # Martin Luther King Jr. Day  
    # '2024-02-19',  # Presidents Day  
    # '2024-03-29',  # Good Friday  
    # '2024-05-27',  # Memorial Day  
    # '2024-06-19',  # Juneteenth  
    # '2024-07-04',  # Independence Day  
    # '2024-09-02',  # Labor Day  
    # '2024-11-28',  # Thanksgiving Day  
    # '2024-12-25',  # Christmas Day  
    # '2025-01-01',  # New Year's Day  
    # '2025-01-20',  # Martin Luther King Jr. Day  
    # '2025-02-17',  # Presidents Day  
]

dates = dates[~dates.isin(holidays)][:400]

df = pd.DataFrame(\[  
    {'date': date, 'symbol_id': symbol_id,  
     'accel': np.clip(1.25 + 0.75 _ np.sin(i \_ 0.1 + symbol_id) + np.random.normal(0, 0.1), 0.5, 2.0)}  
    for symbol_id in [1, 2] for i, date in enumerate(dates)  
])

def get_freq(df): # Exact method mentioned in Tutorial for Custom Freq  
    dates = pd.DatetimeIndex(sorted(df['date'].unique())) # sort all dates in the dataset  
    nyse = mcal.get_calendar('NYSE') # New Yor Stock Exchange calendar  
    trading_days = nyse.valid_days(start_date=dates.min(), end_date="2026-01-01").tz_localize(None)  
    all_weekdays = pd.date_range(start=dates.min(), end="2026-01-01", freq='B')  
    closed_days = all_weekdays.difference(trading_days)  
    custom_bday = pd.offsets.CustomBusinessDay(holidays=closed_days)  
    return custom_bday

def get_forecasted_df(df, finetune_steps, finetune_depth, quantiles):  
    return nixtla_client.forecast(  
        df=df, h=1, time_col='date', target_col="accel", freq=get_freq(df),  
        finetune_steps=finetune_steps, finetune_depth=finetune_depth,  
        id_col='symbol_id', quantiles=quantiles  
    )

forecasted_df = get_forecasted_df(df, finetune_steps=25, finetune_depth=1, quantiles=[0.25, 0.5, 0.75, 0.99])  
print(forecasted_df.head())

-> When I am not using the holidays (basically not removing the holidays, forecasting is running as expected)

Any help would be appreciated!

Thanks & Regards,