Repository navigation
Expand file tree
/
Copy pathread_gbq_simple.py
More file actions
61 lines (52 loc) · 1.92 KB
/
Copy pathread_gbq_simple.py
File metadata and controls
61 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_pandas_gbq_read_gbq_simple]
import bigframes.pandas as bpd
import pandas as pd
import pandas_gbq
# Set partial ordering mode for BigQuery DataFrames.
bpd.options.bigquery.ordering_mode = "partial"
def read_gbq_simple_pandas_gbq() -> pd.DataFrame:
"""Queries BigQuery using pandas-gbq and returns an in-memory DataFrame."""
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
"""
df_pandas = pandas_gbq.read_gbq(sql)
print("Queried using pandas-gbq.")
return df_pandas
def read_gbq_simple_bigframes() -> bpd.DataFrame:
"""Queries BigQuery using BigQuery DataFrames and returns a distributed DataFrame."""
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
"""
df_bigframes = bpd.read_gbq(sql)
print("Queried using BigQuery DataFrames.")
return df_bigframes
# [Preferred] Run using pandas-gbq:
# df_pandas = read_gbq_simple_pandas_gbq()
# print(df_pandas.head())
# Alternatively, run using BigQuery DataFrames:
# df_bigframes = read_gbq_simple_bigframes()
# print(df_bigframes.head())
# [END bigquery_pandas_gbq_read_gbq_simple]