Skip to content

docs(bigquery/bigframes): add load_table_dataframe code sample - #14508

Closed
shuoweil wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
shuoweil:shuowei-bigframes-load-table-sample
Closed

docs(bigquery/bigframes): add load_table_dataframe code sample#14508
shuoweil wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
shuoweil:shuowei-bigframes-load-table-sample

Conversation

@shuoweil

Copy link
Copy Markdown
Contributor

Description

Adds a BigQuery DataFrames (bigframes) code sample demonstrating how to load a pandas DataFrame into a BigQuery table, targeting the documentation page bigquery-load-table-dataframe.

Fixes b/546158350 (Subtask of b/522898394)

Summary of Changes

  • Added bigquery/bigframes/load_table_dataframe.py demonstrating converting a local pandas DataFrame to BigQuery DataFrames and writing to BigQuery using to_gbq.
  • Added corresponding unit test load_table_dataframe_test.py.

@shuoweil
shuoweil requested review from a team and tswast as code owners August 13, 2026 21:26
@snippet-bot

snippet-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples. labels Aug 13, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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).

Suggested change
import pytz
from zoneinfo import ZoneInfo

Comment on lines +34 to +72
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),
},
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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),
        },
    ]

@shuoweil
shuoweil force-pushed the shuowei-bigframes-load-table-sample branch from 216b6bb to 448c556 Compare August 13, 2026 21:31
@shuoweil

Copy link
Copy Markdown
Contributor Author

Will check in the code change in Git-on-Borg.

@shuoweil shuoweil closed this Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant