TimeGPT requires time series data without missing values. While you may have multiple series starting and ending on different dates, each one must maintain a continuous data sequence.

This tutorial shows you how to handle missing values for use with TimeGPT.

For reference, this tutorial is based on the skforecast tutorial:
Forecasting Time Series with Missing Values.

Managing missing values ensures your forecasts with TimeGPT are accurate and reliable.
When dates or values are missing, fill or interpolate them according to the nature of your dataset.

1. Load Data

Load the daily bike rental counts dataset using pandas. Note that the original column names are in Spanish; you will rename them to match ds and y.

Load Data Example
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/JoaquinAmatRodrigo/Estadistica-machine-learning-python/master/data/usuarios_diarios_bicimad.csv')
df = df[['fecha', 'Usos bicis total día']]
df.rename(columns={'fecha': 'ds', 'Usos bicis total día': 'y'}, inplace=True)
df.head()
dsy
02014-06-2399
12014-06-2472
22014-06-25119
32014-06-26135
42014-06-27149

Next, convert your dates to timestamps and assign a unique identifier (unique_id) to handle multiple series if needed:

Convert Dates and Assign ID
df['ds'] = pd.to_datetime(df['ds'])
df['unique_id'] = 'id1'
df = df[['unique_id', 'ds', 'y']]

Reserve the last 93 days for testing:

Reserve Test Data
train_df = df[:-93]
test_df = df[-93:]

To simulate missing data, remove specific date ranges from the training dataset:

Simulate Missing Data
mask = ~((train_df['ds'] >= '2020-09-01') & (train_df['ds'] <= '2020-10-10')) & \
       ~((train_df['ds'] >= '2020-11-08') & (train_df['ds'] <= '2020-12-15'))
train_df_gaps = train_df[mask]

2. Get Started with TimeGPT

Initialize a NixtlaClient object with your Nixtla API key:

Initialize NixtlaClient
from nixtla import NixtlaClient

nixtla_client = NixtlaClient(api_key='my_api_key_provided_by_nixtla')

To use an Azure AI endpoint, explicitly provide the base_url:

Initialize NixtlaClient with Azure
nixtla_client = NixtlaClient(base_url="your_azure_ai_endpoint", api_key="your_api_key")

See the docs on Setting Up Your API Key for details.


3. Visualize Data

Plot your dataset and examine the gaps introduced above:

Plot Data with Gaps
nixtla_client.plot(train_df_gaps)

You can also zoom in to see gap regions more clearly:

Zoomed Plot of Gaps
nixtla_client.plot(train_df_gaps, max_insample_length=800)

Zero-count days often align with specific events, like COVID-19 lockdown constraints.


4. Fill Missing Values

You can use fill_gaps from utilsforecast to insert the missing dates:

Fill Missing Dates with fill_gaps
from utilsforecast.preprocessing import fill_gaps

print('Number of rows before filling gaps:', len(train_df_gaps))
train_df_complete = fill_gaps(train_df_gaps, freq='D')
print('Number of rows after filling gaps:', len(train_df_complete))

Next, handle the newly inserted missing values by interpolation:

Interpolate Missing Values
train_df_complete['y'] = train_df_complete['y'].interpolate(method='linear', limit_direction='both')
train_df_complete.isna().sum()

5. Forecast with TimeGPT

Use the timegpt-1-long-horizon model to generate forecasts:

Generate Forecast
fcst = nixtla_client.forecast(
    train_df_complete,
    h=len(test_df),
    model='timegpt-1-long-horizon'
)

Visualize the forecasts against the actual test data:

Plot Forecast and Test Data
nixtla_client.plot(test_df, fcst)

Forecast comparison between the test dataset and TimeGPT predictions

Evaluate performance (MAE) using utilsforecast:

Evaluate Forecast MAE
from utilsforecast.evaluation import evaluate
from utilsforecast.losses import mae

fcst['ds'] = pd.to_datetime(fcst['ds'])
result = test_df.merge(fcst, on=['ds', 'unique_id'], how='left')

evaluate(result, metrics=[mae])
unique_idmetricTimeGPT
0id1mae1824.693059

6. Important Considerations

• Always ensure that your data is free of missing dates and values before forecasting with TimeGPT.
• Select a gap-filling strategy based on your domain knowledge (linear interpolation, constant filling, etc.).


7. References