Skip to content

Commit 02984a4

Browse files
authored
test: add sample code for use BigFrames developer guide (#118)
* test: add sample code for use BigFrames developer guide
1 parent a669919 commit 02984a4

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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_load_data_from_bigquery():
17+
# [START bigquery_dataframes_load_data_from_bigquery]
18+
# Create a DataFrame from a BigQuery table:
19+
import bigframes.pandas as bpd
20+
21+
query_or_table = "bigquery-public-data.ml_datasets.penguins"
22+
bq_df = bpd.read_gbq(query_or_table)
23+
# [END bigquery_dataframes_load_data_from_bigquery]
24+
assert bq_df is not None
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_load_data_from_csv():
17+
# [START bigquery_dataframes_load_data_from_csv]
18+
import bigframes.pandas as bpd
19+
20+
filepath_or_buffer = "gs://cloud-samples-data/bigquery/us-states/us-states.csv"
21+
df_from_gcs = bpd.read_csv(filepath_or_buffer)
22+
# Display the first few rows of the DataFrame:
23+
df_from_gcs.head()
24+
# [END bigquery_dataframes_load_data_from_csv]
25+
assert df_from_gcs is not None
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_pandas_methods():
17+
# [START bigquery_dataframes_pandas_methods]
18+
import bigframes.pandas as bpd
19+
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"].head(10)
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+
# Calculate the mean body_mass_g by species using the groupby operation:
32+
bq_df["body_mass_g"].groupby(by=bq_df["species"]).mean().head()
33+
# [END bigquery_dataframes_pandas_methods]
34+
assert average_body_mass is not None

‎samples/snippets/set_options_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_set_options():
17+
# Close the session before resetting the options
18+
import bigframes.pandas as bpd
19+
20+
bpd.close_session()
21+
22+
# [START bigquery_dataframes_set_options]
23+
import bigframes.pandas as bpd
24+
25+
PROJECT_ID = "bigframes-dec" # @param {type:"string"}
26+
REGION = "US" # @param {type:"string"}
27+
28+
# Set BigQuery DataFrames options
29+
bpd.options.bigquery.project = PROJECT_ID
30+
bpd.options.bigquery.location = REGION
31+
32+
# [END bigquery_dataframes_set_options]
33+
assert bpd.options.bigquery.project == PROJECT_ID
34+
assert bpd.options.bigquery.location == REGION

0 commit comments

Comments
 (0)