|
| 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 | + |
| 16 | +# [START bigquery_dataframes_call_python_udf] |
| 17 | +import textwrap |
| 18 | +from typing import Tuple |
| 19 | + |
| 20 | +import bigframes.pandas as bpd |
| 21 | +import pandas as pd |
| 22 | +import pyarrow as pa |
| 23 | + |
| 24 | + |
| 25 | +# Using partial ordering mode enables more efficient query optimizations. |
| 26 | +bpd.options.bigquery.ordering_mode = "partial" |
| 27 | + |
| 28 | + |
| 29 | +def call_python_udf( |
| 30 | + project_id: str, location: str, |
| 31 | +) -> Tuple[pd.Series, bpd.Series]: |
| 32 | + # Set the billing project to use for queries. This step is optional, as the |
| 33 | + # project can be inferred from your environment in many cases. |
| 34 | + bpd.options.bigquery.project = project_id # "your-project-id" |
| 35 | + |
| 36 | + # Since this example works with local data, set a processing location. |
| 37 | + bpd.options.bigquery.location = location # "US" |
| 38 | + |
| 39 | + # Create a sample series. |
| 40 | + xml_series = pd.Series( |
| 41 | + [ |
| 42 | + textwrap.dedent( |
| 43 | + """ |
| 44 | + <book id="1"> |
| 45 | + <title>The Great Gatsby</title> |
| 46 | + <author>F. Scott Fitzgerald</author> |
| 47 | + </book> |
| 48 | + """ |
| 49 | + ), |
| 50 | + textwrap.dedent( |
| 51 | + """ |
| 52 | + <book id="2"> |
| 53 | + <title>1984</title> |
| 54 | + <author>George Orwell</author> |
| 55 | + </book> |
| 56 | + """ |
| 57 | + ), |
| 58 | + textwrap.dedent( |
| 59 | + """ |
| 60 | + <book id="3"> |
| 61 | + <title>Brave New World</title> |
| 62 | + <author>Aldous Huxley</author> |
| 63 | + </book> |
| 64 | + """ |
| 65 | + ), |
| 66 | + ], |
| 67 | + dtype=pd.ArrowDtype(pa.string()), |
| 68 | + ) |
| 69 | + df = pd.DataFrame({"xml": xml_series}) |
| 70 | + |
| 71 | + # Use the BigQuery Accessor, which is automatically registered on pandas |
| 72 | + # DataFrames when you import bigframes. This example uses a function that |
| 73 | + # has been deployed to bigquery-utils for demonstration purposes. To use in |
| 74 | + # production, deploy the function at |
| 75 | + # https://github.com/GoogleCloudPlatform/bigquery-utils/blob/master/udfs/community/cw_xml_extract.sqlx |
| 76 | + # to your own project. |
| 77 | + titles_pandas = df.bigquery.sql_scalar( |
| 78 | + "`bqutil`.`fn`.cw_xml_extract({xml}, '//title/text()')", |
| 79 | + ) |
| 80 | + |
| 81 | + # Alternatively, call read_gbq_function to get a pointer to the function |
| 82 | + # that can be applied on BigQuery DataFrames objects. |
| 83 | + cw_xml_extract = bpd.read_gbq_function("bqutil.fn.cw_xml_extract") |
| 84 | + xml_bigframes = bpd.read_pandas(xml_series) |
| 85 | + |
| 86 | + xpath_query = "//title/text()" |
| 87 | + titles_bigframes = xml_bigframes.apply(cw_xml_extract, args=(xpath_query,)) |
| 88 | + return titles_pandas, titles_bigframes |
| 89 | + # [END bigquery_dataframes_call_python_udf] |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + import argparse |
| 94 | + |
| 95 | + parser = argparse.ArgumentParser() |
| 96 | + |
| 97 | + # Note: GCP project ID can be inferred from the environment if Application |
| 98 | + # Default Credentials are set, so None is perfectly valid for --project_id. |
| 99 | + parser.add_argument("--project_id", type=str) |
| 100 | + parser.add_argument("--location", default="US", type=str) |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + pddf, bfdf = call_python_udf(project_id=args.project_id, location=args.location) |
| 104 | + print(pddf) |
| 105 | + print(bfdf.to_pandas()) |
0 commit comments