Skip to content

Commit 2b84c4f

Browse files
SalemJordenSalem Boylandtswast
authored
docs: add the first sample for the Single time-series forecasting from Google Analytics data tutorial (#623)
BigQuery DataFrames sample for [Step two (optional): Visualize the time series you want to forecast](https://cloud.google.com/bigquery/docs/arima-single-time-series-forecasting-tutorial#step_two_optional_visualize_the_time_series_you_want_to_forecast). Co-authored-by: Salem Boyland <[email protected]> Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent 4724a1a commit 2b84c4f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (t
4+
# you may not use this file except in compliance wi
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in
10+
# distributed under the License is distributed on a
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, eit
12+
# See the License for the specific language governi
13+
# limitations under the License.
14+
15+
16+
def test_create_single_timeseries():
17+
18+
# [START bigquery_dataframes_single_timeseries_forecasting_model_tutorial]
19+
import bigframes.pandas as bpd
20+
21+
# Start by loading the historical data from BigQuerythat you want to analyze and forecast.
22+
# This clause indicates that you are querying the ga_sessions_* tables in the google_analytics_sample dataset.
23+
# Read and visualize the time series you want to forecast.
24+
df = bpd.read_gbq("bigquery-public-data.google_analytics_sample.ga_sessions_*")
25+
parsed_date = bpd.to_datetime(df.date, format="%Y%m%d", utc=True)
26+
visits = df["totals"].struct.field("visits")
27+
total_visits = visits.groupby(parsed_date).sum()
28+
29+
# Expected output: total_visits.head()
30+
# date
31+
# 2016-08-01 00:00:00+00:00 1711
32+
# 2016-08-02 00:00:00+00:00 2140
33+
# 2016-08-03 00:00:00+00:00 2890
34+
# 2016-08-04 00:00:00+00:00 3161
35+
# 2016-08-05 00:00:00+00:00 2702
36+
# Name: visits, dtype: Int64
37+
38+
total_visits.plot.line()
39+
40+
# [END bigquery_dataframes_single_timeseries_forecasting_model_tutorial]

0 commit comments

Comments
 (0)