{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Copyright 2025 Google LLC\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Struct Data Types\n", "\n", "In BigQuery, a [STRUCT](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type) (also known as a `record`) is a collection of ordered fields, each with a defined data type (required) and an optional field name. BigQuery DataFrames maps BigQuery `STRUCT` types to the pandas equivalent, `pandas.ArrowDtype(pa.struct())`. \n", "\n", "This notebook illustrates how to work with `STRUCT` columns in BigQuery DataFrames. First, let's import the required packages and perform the necessary setup below." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import bigframes.pandas as bpd\n", "import bigframes.bigquery as bbq\n", "import pandas as pd\n", "import pyarrow as pa" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "REGION = \"US\" # @param {type: \"string\"}\n", "\n", "bpd.options.display.progress_bar = None\n", "bpd.options.bigquery.location = REGION" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create DataFrames with struct columns\n", "\n", "**Example 1: Creating from a list of objects**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Name | \n", "Address | \n", "
|---|---|---|
| 0 | \n", "Alice | \n", "{'City': 'New York', 'State': 'NY'} | \n", "
| 1 | \n", "Bob | \n", "{'City': 'San Francisco', 'State': 'CA'} | \n", "
| 2 | \n", "Charlie | \n", "{'City': 'Seattle', 'State': 'WA'} | \n", "
3 rows × 2 columns
\n", "| \n", " | City | \n", "State | \n", "
|---|---|---|
| 0 | \n", "New York | \n", "NY | \n", "
| 1 | \n", "San Francisco | \n", "CA | \n", "
| 2 | \n", "Seattle | \n", "WA | \n", "
3 rows × 2 columns
\n", "| \n", " | Name | \n", "Address.City | \n", "Address.State | \n", "
|---|---|---|---|
| 0 | \n", "Alice | \n", "New York | \n", "NY | \n", "
| 1 | \n", "Bob | \n", "San Francisco | \n", "CA | \n", "
| 2 | \n", "Charlie | \n", "Seattle | \n", "WA | \n", "
3 rows × 3 columns
\n", "