Skip to content

Commit 216b6bb

Browse files
committed
feat(bigframes): add load_table_dataframe code sample (b/546158350)
1 parent ad8d32e commit 216b6bb

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Copyright 2026 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+
# [START bigquery_load_table_dataframe]
16+
import datetime
17+
18+
import pandas as pd
19+
import pytz
20+
21+
import bigframes.pandas as bpd
22+
23+
# Set partial ordering mode as the default configuration for BigQuery
24+
# DataFrames.
25+
bpd.options.bigquery.ordering_mode = "partial"
26+
27+
28+
def load_table_dataframe(
29+
table_id: str = "your-project.your_dataset.your_table_name",
30+
) -> bpd.DataFrame:
31+
# TODO(developer): Set table_id to the ID of the table to create.
32+
# table_id = "your-project.your_dataset.your_table_name"
33+
34+
records = [
35+
{
36+
"title": "The Meaning of Life",
37+
"release_year": 1983,
38+
"length_minutes": 112.5,
39+
"release_date": pytz.timezone("Europe/Paris")
40+
.localize(datetime.datetime(1983, 5, 9, 13, 0, 0))
41+
.astimezone(pytz.utc),
42+
# Assume UTC timezone when a datetime object contains no timezone.
43+
"dvd_release": datetime.datetime(2002, 1, 22, 7, 0, 0),
44+
},
45+
{
46+
"title": "Monty Python and the Holy Grail",
47+
"release_year": 1975,
48+
"length_minutes": 91.5,
49+
"release_date": pytz.timezone("Europe/London")
50+
.localize(datetime.datetime(1975, 4, 9, 23, 59, 2))
51+
.astimezone(pytz.utc),
52+
"dvd_release": datetime.datetime(2002, 7, 16, 9, 0, 0),
53+
},
54+
{
55+
"title": "Life of Brian",
56+
"release_year": 1979,
57+
"length_minutes": 94.25,
58+
"release_date": pytz.timezone("America/New_York")
59+
.localize(datetime.datetime(1979, 8, 17, 23, 59, 5))
60+
.astimezone(pytz.utc),
61+
"dvd_release": datetime.datetime(2008, 1, 14, 8, 0, 0),
62+
},
63+
{
64+
"title": "And Now for Something Completely Different",
65+
"release_year": 1971,
66+
"length_minutes": 88.0,
67+
"release_date": pytz.timezone("Europe/London")
68+
.localize(datetime.datetime(1971, 9, 28, 23, 59, 7))
69+
.astimezone(pytz.utc),
70+
"dvd_release": datetime.datetime(2003, 10, 22, 10, 0, 0),
71+
},
72+
]
73+
dataframe = pd.DataFrame(
74+
records,
75+
# In the loaded table, the column order reflects the order of the
76+
# columns in the DataFrame.
77+
columns=[
78+
"title",
79+
"release_year",
80+
"length_minutes",
81+
"release_date",
82+
"dvd_release",
83+
],
84+
# Optionally, set a named index, which can also be written to the
85+
# BigQuery table.
86+
index=pd.Index(
87+
["Q24980", "Q25043", "Q24953", "Q16403"], name="wikidata_id"
88+
),
89+
)
90+
91+
# Convert the local pandas DataFrame into a BigQuery DataFrame.
92+
bq_df = bpd.read_pandas(dataframe)
93+
94+
# Write the DataFrame to a BigQuery table.
95+
bq_df.to_gbq(table_id, if_exists="replace", index=True)
96+
return bq_df
97+
# [END bigquery_load_table_dataframe]
98+
99+
100+
if __name__ == "__main__":
101+
import os
102+
103+
table_id = os.environ.get(
104+
"TABLE_ID", "your-project.your_dataset.your_table_name"
105+
)
106+
print(load_table_dataframe(table_id=table_id))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2026 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+
import load_table_dataframe
16+
17+
18+
def test_load_table_dataframe(project_id: str, dataset_id: str) -> None:
19+
table_id = f"{project_id}.{dataset_id}.load_table_dataframe"
20+
bq_df = load_table_dataframe.load_table_dataframe(table_id=table_id)
21+
assert bq_df is not None
22+
assert len(bq_df) == 4

0 commit comments

Comments
 (0)