Skip to content

Commit 7cbbb7d

Browse files
authored
docs: add code snippets for explore query result page (#278)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/python-bigquery-dataframes/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes internal issue 316614454 🦕
1 parent 9798a2b commit 7cbbb7d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_explore_query_result():
17+
import bigframes.pandas as bpd
18+
19+
# [START bigquery_dataframes_explore_query_result]
20+
# Load data from BigQuery
21+
query_or_table = "bigquery-public-data.ml_datasets.penguins"
22+
bq_df = bpd.read_gbq(query_or_table)
23+
24+
# Inspect one of the columns (or series) of the DataFrame:
25+
bq_df["body_mass_g"]
26+
27+
# Compute the mean of this series:
28+
average_body_mass = bq_df["body_mass_g"].mean()
29+
print(f"average_body_mass: {average_body_mass}")
30+
31+
# Find the heaviest species using the groupby operation to calculate the
32+
# mean body_mass_g:
33+
(
34+
bq_df["body_mass_g"]
35+
.groupby(by=bq_df["species"])
36+
.mean()
37+
.sort_values(ascending=False)
38+
.head(10)
39+
)
40+
41+
# Create the Linear Regression model
42+
from bigframes.ml.linear_model import LinearRegression
43+
44+
# Filter down to the data we want to analyze
45+
adelie_data = bq_df[bq_df.species == "Adelie Penguin (Pygoscelis adeliae)"]
46+
47+
# Drop the columns we don't care about
48+
adelie_data = adelie_data.drop(columns=["species"])
49+
50+
# Drop rows with nulls to get our training data
51+
training_data = adelie_data.dropna()
52+
53+
# Pick feature columns and label column
54+
X = training_data[
55+
[
56+
"island",
57+
"culmen_length_mm",
58+
"culmen_depth_mm",
59+
"flipper_length_mm",
60+
"sex",
61+
]
62+
]
63+
y = training_data[["body_mass_g"]]
64+
65+
model = LinearRegression(fit_intercept=False)
66+
model.fit(X, y)
67+
model.score(X, y)
68+
# [END bigquery_dataframes_explore_query_result]
69+
assert average_body_mass is not None
70+
assert model is not None

0 commit comments

Comments
 (0)