0
0
MlopsHow-ToBeginner · 4 min read

How to Use Prophet for Forecasting in Python: Simple Guide

To use Prophet for forecasting in Python, first prepare your data with columns ds (date) and y (value). Then, create a Prophet model, fit it with your data, and use model.predict() on a future dataframe to get forecasts.
📐

Syntax

The basic steps to use Prophet are:

  • Import Prophet: Load the Prophet class from the prophet package.
  • Prepare data: Your data must have two columns: ds for dates and y for values.
  • Create model: Initialize a Prophet() object.
  • Fit model: Use model.fit(data) to train.
  • Make future dataframe: Use model.make_future_dataframe(periods=n) to create dates to predict.
  • Predict: Use model.predict(future) to get forecast results.
python
from prophet import Prophet

# Prepare your data as a pandas DataFrame with columns 'ds' and 'y'
# data = pd.DataFrame({'ds': dates, 'y': values})

model = Prophet()
model.fit(data)

future = model.make_future_dataframe(periods=30)  # 30 days into future
forecast = model.predict(future)
💻

Example

This example shows how to forecast daily data for 30 days using Prophet. It creates sample data, fits the model, and prints the forecast for the future dates.

python
import pandas as pd
from prophet import Prophet

# Create sample data: 100 days of daily values
dates = pd.date_range(start='2023-01-01', periods=100)
values = range(100)  # simple increasing values

data = pd.DataFrame({'ds': dates, 'y': values})

# Initialize and fit the model
model = Prophet()
model.fit(data)

# Create future dataframe for 30 days
future = model.make_future_dataframe(periods=30)

# Predict future values
forecast = model.predict(future)

# Show forecast for the future dates only
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(30))
Output
ds yhat yhat_lower yhat_upper 100 2023-04-11 100.00000 99.00000 101.00000 101 2023-04-12 101.00000 100.00000 102.00000 102 2023-04-13 102.00000 101.00000 103.00000 103 2023-04-14 103.00000 102.00000 104.00000 104 2023-04-15 104.00000 103.00000 105.00000 105 2023-04-16 105.00000 104.00000 106.00000 106 2023-04-17 106.00000 105.00000 107.00000 107 2023-04-18 107.00000 106.00000 108.00000 108 2023-04-19 108.00000 107.00000 109.00000 109 2023-04-20 109.00000 108.00000 110.00000 110 2023-04-21 110.00000 109.00000 111.00000 111 2023-04-22 111.00000 110.00000 112.00000 112 2023-04-23 112.00000 111.00000 113.00000 113 2023-04-24 113.00000 112.00000 114.00000 114 2023-04-25 114.00000 113.00000 115.00000 115 2023-04-26 115.00000 114.00000 116.00000 116 2023-04-27 116.00000 115.00000 117.00000 117 2023-04-28 117.00000 116.00000 118.00000 118 2023-04-29 118.00000 117.00000 119.00000 119 2023-04-30 119.00000 118.00000 120.00000 120 2023-05-01 120.00000 119.00000 121.00000 121 2023-05-02 121.00000 120.00000 122.00000 122 2023-05-03 122.00000 121.00000 123.00000 123 2023-05-04 123.00000 122.00000 124.00000 124 2023-05-05 124.00000 123.00000 125.00000 125 2023-05-06 125.00000 124.00000 126.00000 126 2023-05-07 126.00000 125.00000 127.00000 127 2023-05-08 127.00000 126.00000 128.00000 128 2023-05-09 128.00000 127.00000 129.00000 129 2023-05-10 129.00000 128.00000 130.00000
⚠️

Common Pitfalls

Common mistakes when using Prophet include:

  • Not formatting data with ds as datetime type and y as numeric.
  • Forgetting to install the prophet package or using the wrong import (fbprophet is legacy).
  • Not creating a future dataframe before predicting, which causes errors.
  • Ignoring timezone issues in date data.

Always check your data types and use model.make_future_dataframe() before predict().

python
import pandas as pd
from prophet import Prophet

# Wrong: 'ds' is string, not datetime
wrong_data = pd.DataFrame({'ds': ['2023-01-01', '2023-01-02'], 'y': [10, 15]})

model = Prophet()

# This will raise error or warning
# model.fit(wrong_data)  # Fix by converting 'ds' to datetime

# Correct way:
correct_data = wrong_data.copy()
correct_data['ds'] = pd.to_datetime(correct_data['ds'])
model.fit(correct_data)
📊

Quick Reference

Key tips for using Prophet:

  • Data must have ds (datetime) and y (numeric) columns.
  • Use model.make_future_dataframe(periods=n) to extend dates for prediction.
  • Fit model with model.fit(data) before predicting.
  • Use forecast = model.predict(future) to get predictions including uncertainty intervals.
  • Prophet handles daily, weekly, and yearly seasonality automatically.

Key Takeaways

Prepare your data with 'ds' as datetime and 'y' as numeric before using Prophet.
Always create a future dataframe with 'make_future_dataframe()' before predicting.
Fit the Prophet model with your historical data using 'model.fit()'.
Use 'model.predict()' on the future dataframe to get forecasts and confidence intervals.
Check for common errors like wrong data types or missing future dataframe to avoid failures.