docs(bigquery/bigframes): add load_table_dataframe code sample - #14508
docs(bigquery/bigframes): add load_table_dataframe code sample#14508shuoweil wants to merge 1 commit into
Conversation
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new script and associated test to demonstrate loading a local pandas DataFrame into a BigQuery table using BigQuery DataFrames. The review feedback recommends replacing the deprecated pytz library with the standard library's zoneinfo module for cleaner and more modern timezone handling.
| import datetime | ||
|
|
||
| import pandas as pd | ||
| import pytz |
There was a problem hiding this comment.
Since Python 3.9, the standard library includes the zoneinfo module, which is the modern and recommended way to handle time zones. Using zoneinfo avoids the need for the external pytz library, which is deprecated and has known quirks (such as requiring the .localize() method instead of passing the timezone directly to the datetime constructor).
| import pytz | |
| from zoneinfo import ZoneInfo |
| records = [ | ||
| { | ||
| "title": "The Meaning of Life", | ||
| "release_year": 1983, | ||
| "length_minutes": 112.5, | ||
| "release_date": pytz.timezone("Europe/Paris") | ||
| .localize(datetime.datetime(1983, 5, 9, 13, 0, 0)) | ||
| .astimezone(pytz.utc), | ||
| # Assume UTC timezone when a datetime object contains no timezone. | ||
| "dvd_release": datetime.datetime(2002, 1, 22, 7, 0, 0), | ||
| }, | ||
| { | ||
| "title": "Monty Python and the Holy Grail", | ||
| "release_year": 1975, | ||
| "length_minutes": 91.5, | ||
| "release_date": pytz.timezone("Europe/London") | ||
| .localize(datetime.datetime(1975, 4, 9, 23, 59, 2)) | ||
| .astimezone(pytz.utc), | ||
| "dvd_release": datetime.datetime(2002, 7, 16, 9, 0, 0), | ||
| }, | ||
| { | ||
| "title": "Life of Brian", | ||
| "release_year": 1979, | ||
| "length_minutes": 94.25, | ||
| "release_date": pytz.timezone("America/New_York") | ||
| .localize(datetime.datetime(1979, 8, 17, 23, 59, 5)) | ||
| .astimezone(pytz.utc), | ||
| "dvd_release": datetime.datetime(2008, 1, 14, 8, 0, 0), | ||
| }, | ||
| { | ||
| "title": "And Now for Something Completely Different", | ||
| "release_year": 1971, | ||
| "length_minutes": 88.0, | ||
| "release_date": pytz.timezone("Europe/London") | ||
| .localize(datetime.datetime(1971, 9, 28, 23, 59, 7)) | ||
| .astimezone(pytz.utc), | ||
| "dvd_release": datetime.datetime(2003, 10, 22, 10, 0, 0), | ||
| }, | ||
| ] |
There was a problem hiding this comment.
Use standard library zoneinfo.ZoneInfo and datetime.timezone.utc instead of pytz to localize the datetimes. This is cleaner, more idiomatic, and avoids the deprecated pytz library.
records = [
{
"title": "The Meaning of Life",
"release_year": 1983,
"length_minutes": 112.5,
"release_date": datetime.datetime(
1983, 5, 9, 13, 0, 0, tzinfo=ZoneInfo("Europe/Paris")
).astimezone(datetime.timezone.utc),
# Assume UTC timezone when a datetime object contains no timezone.
"dvd_release": datetime.datetime(2002, 1, 22, 7, 0, 0),
},
{
"title": "Monty Python and the Holy Grail",
"release_year": 1975,
"length_minutes": 91.5,
"release_date": datetime.datetime(
1975, 4, 9, 23, 59, 2, tzinfo=ZoneInfo("Europe/London")
).astimezone(datetime.timezone.utc),
"dvd_release": datetime.datetime(2002, 7, 16, 9, 0, 0),
},
{
"title": "Life of Brian",
"release_year": 1979,
"length_minutes": 94.25,
"release_date": datetime.datetime(
1979, 8, 17, 23, 59, 5, tzinfo=ZoneInfo("America/New_York")
).astimezone(datetime.timezone.utc),
"dvd_release": datetime.datetime(2008, 1, 14, 8, 0, 0),
},
{
"title": "And Now for Something Completely Different",
"release_year": 1971,
"length_minutes": 88.0,
"release_date": datetime.datetime(
1971, 9, 28, 23, 59, 7, tzinfo=ZoneInfo("Europe/London")
).astimezone(datetime.timezone.utc),
"dvd_release": datetime.datetime(2003, 10, 22, 10, 0, 0),
},
]216b6bb to
448c556
Compare
|
Will check in the code change in Git-on-Borg. |
Description
Adds a BigQuery DataFrames (
bigframes) code sample demonstrating how to load a pandas DataFrame into a BigQuery table, targeting the documentation pagebigquery-load-table-dataframe.Fixes b/546158350 (Subtask of b/522898394)
Summary of Changes
bigquery/bigframes/load_table_dataframe.pydemonstrating converting a local pandas DataFrame to BigQuery DataFrames and writing to BigQuery usingto_gbq.load_table_dataframe_test.py.