|
| 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