prophet¶
prophet(records, periods, kwargs)
Description¶
Return Time Series Forecast as json using prophet python library.
Parameters of this function are passed as is to prophet python function and python function result is returned as is.
(Inspired from this Felipe Hoffa medium's post)
Usage¶
Call or Deploy prophet
?
Call prophet
directly
The easiest way to use bigfunctions
prophet
function is deployed in 39 public datasets for all of the 39 BigQuery regions.- It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)
Public BigFunctions Datasets
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Deploy prophet
in your project
Why deploy?
- You may prefer to deploy
prophet
in your own project to build and manage your own catalog of functions. - This is particularly useful if you want to create private functions (for example calling your internal APIs).
- Get started by reading the framework page
Deployment
prophet
function can be deployed with:
pip install bigfunctions
bigfun get prophet
bigfun deploy prophet
Examples¶
1. Using a table with columns named ds
and y
for date and value respectively
with sample_data as (
select date('2022-01-01') as ds, 1 as y,
union all
select date('2022-01-02') as ds, 2 as y,
)
select bigfunctions.eu.prophet(to_json(array_agg(sample_data)), 3, null)
from sample_data
with sample_data as (
select date('2022-01-01') as ds, 1 as y,
union all
select date('2022-01-02') as ds, 2 as y,
)
select bigfunctions.us.prophet(to_json(array_agg(sample_data)), 3, null)
from sample_data
with sample_data as (
select date('2022-01-01') as ds, 1 as y,
union all
select date('2022-01-02') as ds, 2 as y,
)
select bigfunctions.europe_west1.prophet(to_json(array_agg(sample_data)), 3, null)
from sample_data
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| forecasted_records |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [
{"ds": "2022-01-03", "yhat": 3, "yhat_upper": 3, ...},
{"ds": "2022-01-04", "yhat": 4, "yhat_upper": 4, ...},
{"ds": "2022-01-05", "yhat": 5, "yhat_upper": 5, ...}
]
|
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2. Using a table with columns to rename to ds
and y
with sample_data as (
select date('2022-01-01') as date, 1 as value,
union all
select date('2022-01-02') as date, 2 as value,
)
select bigfunctions.eu.prophet(to_json(array_agg(struct(date as ds, value as y))), 3, null)
from sample_data
with sample_data as (
select date('2022-01-01') as date, 1 as value,
union all
select date('2022-01-02') as date, 2 as value,
)
select bigfunctions.us.prophet(to_json(array_agg(struct(date as ds, value as y))), 3, null)
from sample_data
with sample_data as (
select date('2022-01-01') as date, 1 as value,
union all
select date('2022-01-02') as date, 2 as value,
)
select bigfunctions.europe_west1.prophet(to_json(array_agg(struct(date as ds, value as y))), 3, null)
from sample_data
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| forecasted_records |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [
{"ds": "2022-01-03", "yhat": 3, "yhat_upper": 3, ...},
{"ds": "2022-01-04", "yhat": 4, "yhat_upper": 4, ...},
{"ds": "2022-01-05", "yhat": 5, "yhat_upper": 5, ...}
]
|
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Use cases¶
A use case for this prophet
BigQuery function would be forecasting future sales based on historical sales data. Imagine you have a table in BigQuery called sales_data
with two columns: date
(DATE) and sales
(INTEGER). You want to predict sales for the next 7 days.
SELECT bigfunctions.<your-region>.prophet(
(
SELECT
JSON_ARRAY(CAST(date AS STRING), sales)
FROM
`your-project.your_dataset.sales_data`
ORDER BY
date
),
7
) AS forecasted_sales;
Replace <your-region>
with the appropriate BigQuery region for your dataset (e.g., us
, eu
, us-central1
). This query will:
-
Prepare the input data: The subquery selects the date and sales data from your
sales_data
table, converts the date to a string, and usesJSON_ARRAY
to create an array of [date, sales] pairs for each row. This is the format expected by theprophet
function. The data is ordered by date, which is crucial for time series forecasting. -
Call the prophet function: The
prophet
function is called with the JSON array of historical data and the number of periods (7 days) to forecast. -
Return the forecast: The function returns a JSON array containing the forecasted sales for the next 7 days in the same [date, sales] format. The result is aliased as
forecasted_sales
.
You can then use the forecasted sales data for inventory planning, resource allocation, and other business decisions.
More advanced example with custom seasonality:
You can also pass additional parameters to the underlying Prophet model using the kwargs
argument. For example, to add a weekly seasonality:
SELECT bigfunctions.<your-region>.prophet(
(
SELECT
JSON_ARRAY(CAST(date AS STRING), sales)
FROM
`your-project.your_dataset.sales_data`
ORDER BY
date
),
7,
STRUCT(JSON'{"weekly_seasonality": true}' as kwargs)
) AS forecasted_sales_with_weekly_seasonality;
This allows you to customize the model to better fit your specific data and business needs, such as accounting for daily, weekly, or yearly seasonality. Refer to the Prophet documentation for a complete list of available parameters.
This example demonstrates how the prophet
BigQuery function can be used for practical time series forecasting directly within BigQuery, simplifying the process and leveraging the power of Prophet without needing external libraries or tools.
Need help or Found a bug?
Get help using prophet
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about prophet
If the function does not work as expected, please
- report a bug so that it can be improved.
- or open the discussion with the community on Slack.
We also provide professional suppport.