Skip to content

Commit f54e768

Browse files
rey-esptswast
andauthored
docs: add samples using SQL methods via the bigframes.bigquery module (#1358)
* docs: add bigquery_modeules_test.py file with bigframes.bigquery examples * add asserts * snippet prefix * rename and space * fix snippet names * fix snippet names * edit sninppet groups * Update samples/snippets/bigquery_modules_test.py * Update samples/snippets/bigquery_modules_test.py --------- Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent c6c9120 commit f54e768

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
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 writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_bigquery_dataframes_examples() -> None:
17+
# [START bigquery_dataframes_bigquery_methods_struct]
18+
import bigframes.bigquery as bbq
19+
import bigframes.pandas as bpd
20+
21+
# Load data from BigQuery
22+
query_or_table = "bigquery-public-data.ml_datasets.penguins"
23+
bq_df = bpd.read_gbq(query_or_table)
24+
25+
# Create a new STRUCT Series with subfields for each column in a DataFrames.
26+
lengths = bbq.struct(
27+
bq_df[["culmen_length_mm", "culmen_depth_mm", "flipper_length_mm"]]
28+
)
29+
30+
lengths.peek()
31+
# 146 {'culmen_length_mm': 51.1, 'culmen_depth_mm': ...
32+
# 278 {'culmen_length_mm': 48.2, 'culmen_depth_mm': ...
33+
# 337 {'culmen_length_mm': 36.4, 'culmen_depth_mm': ...
34+
# 154 {'culmen_length_mm': 46.5, 'culmen_depth_mm': ...
35+
# 185 {'culmen_length_mm': 50.1, 'culmen_depth_mm': ...
36+
# dtype: struct[pyarrow]
37+
# [END bigquery_dataframes_bigquery_methods_struct]
38+
39+
# [START bigquery_dataframes_bigquery_methods_scalar]
40+
import bigframes.bigquery as bbq
41+
import bigframes.pandas as bpd
42+
43+
# Load data from BigQuery
44+
query_or_table = "bigquery-public-data.ml_datasets.penguins"
45+
46+
# The sql_scalar function can be used to inject SQL syntax that is not supported
47+
# or difficult to express with the bigframes.pandas APIs.
48+
bq_df = bpd.read_gbq(query_or_table)
49+
shortest = bbq.sql_scalar(
50+
"LEAST({0}, {1}, {2})",
51+
columns=[
52+
bq_df["culmen_depth_mm"],
53+
bq_df["culmen_length_mm"],
54+
bq_df["flipper_length_mm"],
55+
],
56+
)
57+
58+
shortest.peek()
59+
# 0
60+
# 149 18.9
61+
# 33 16.3
62+
# 296 17.2
63+
# 287 17.0
64+
# 307 15.0
65+
# dtype: Float64
66+
# [END bigquery_dataframes_bigquery_methods_scalar]
67+
assert bq_df is not None
68+
assert lengths is not None
69+
assert shortest is not None

0 commit comments

Comments
 (0)