Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions bigquery/bigframes/query_results_dataframe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START bigquery_bigframes_query_results_dataframe]
import bigframes.pandas as bpd

# Set partial ordering mode as the default configuration for BigQuery
# DataFrames.
bpd.options.bigquery.ordering_mode = "partial"


def query_results_dataframe() -> bpd.DataFrame:
sql = """
Comment on lines +18 to +24

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

Setting global configuration options like bpd.options.bigquery.ordering_mode at the module level causes side effects immediately when the module is imported. This can unexpectedly alter the behavior of other parts of an application that imports this module. It is safer and more idiomatic to set this configuration inside the function so that it only executes when the function is called.

def query_results_dataframe() -> bpd.DataFrame:
    # Set partial ordering mode as the default configuration for BigQuery
    # DataFrames.
    bpd.options.bigquery.ordering_mode = "partial"

    sql = """

SELECT name, SUM(number) as total_people
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE state = 'TX'
GROUP BY name
ORDER BY total_people DESC
LIMIT 20
"""

# Run query and load results directly into a BigQuery DataFrame.
df = bpd.read_gbq(sql)
return df
# [END bigquery_bigframes_query_results_dataframe]


if __name__ == "__main__":
df = query_results_dataframe()
print(df.head())
22 changes: 22 additions & 0 deletions bigquery/bigframes/query_results_dataframe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import query_results_dataframe


def test_query_results_dataframe() -> None:
df = query_results_dataframe.query_results_dataframe()
assert df is not None
assert len(df) == 20
assert "total_people" in df.columns