BigQuery DataFrames は、メタデータの管理に内部的にローカル セッション オブジェクトを使用します。各 DataFrame オブジェクトと Series オブジェクトはセッションに接続され、各セッションはロケーションに接続されます。セッション内の各クエリは、セッションを作成したロケーションで実行されます。次のコードサンプルを使用して、セッションを手動で作成し、データの読み込みに使用します。
importbigframesimportbigframes.pandasasbpd# Create session objectcontext=bigframes.BigQueryOptions(project=YOUR_PROJECT_ID,location=YOUR_LOCATION,)session=bigframes.Session(context)# Load a BigQuery table into a dataframedf1=session.read_gbq("bigquery-public-data.ml_datasets.penguins")# Create a dataframe with local data:df2=bpd.DataFrame({"my_col":[1,2,3]},session=session)
importbigframesimportbigframes.pandasasbpdcontext=bigframes.BigQueryOptions(location=YOUR_LOCATION,project=YOUR_PROJECT_ID)session1=bigframes.Session(context)session2=bigframes.Session(context)series1=bpd.Series([1,2,3,4,5],session=session1)series2=bpd.Series([1,2,3,4,5],session=session2)try:series1+series2exceptValueErrorase:print(e)# Error message: Cannot use combine sources from multiple sessions
グローバル セッション
BigQuery DataFrames には、bigframes.pandas.get_global_session() メソッドでアクセスできるデフォルトのグローバル セッションが用意されています。Colab では、bigframes.pandas.options.bigquery.project 属性を使用する前に、その属性のプロジェクト ID を指定する必要があります。bigframes.pandas.options.bigquery.location 属性を使用してロケーションを設定することもできます。デフォルトは US マルチリージョンです。
次のコードサンプルは、グローバル セッションのオプションを設定する方法を示しています。
importbigframes.pandasasbpd# Set project ID for the global sessionbpd.options.bigquery.project=YOUR_PROJECT_ID# Update the global default session locationbpd.options.bigquery.location=YOUR_LOCATION
# The following two statements are essentially the samedf=bpd.read_gbq("bigquery-public-data.ml_datasets.penguins")df=bpd.get_global_session().read_gbq("bigquery-public-data.ml_datasets.penguins")
インメモリ データ
pandas でオブジェクトを作成する方法と同様に、組み込みの Python または NumPy データ構造を使用して Dataframes オブジェクトと Series オブジェクトを作成できます。次のコードサンプルを使用してオブジェクトを作成します。
importnumpyasnpimportbigframes.pandasasbpds=bpd.Series([1,2,3])# Create a dataframe with Python dictdf=bpd.DataFrame({"col_1":[1,2,3],"col_2":[4,5,6],})# Create a series with Numpys=bpd.Series(np.arange(10))
importnumpyasnpimportpandasaspdimportbigframes.pandasasbpdpd_df=pd.DataFrame(np.random.randn(4,2))# Convert Pandas dataframe to BigQuery DataFrame with read_pandas()df_1=bpd.read_pandas(pd_df)# Convert Pandas dataframe to BigQuery DataFrame with the dataframe constructordf_2=bpd.DataFrame(pd_df)
importbigframes.pandasasbpdbf_df=bpd.DataFrame({"my_col":[1,2,3]})# Returns a Pandas Dataframebf_df.to_pandas()bf_s=bpd.Series([1,2,3])# Returns a Pandas Seriesbf_s.to_pandas()
importbigframes.pandasasbpddf=bpd.read_gbq("bigquery-public-data.ml_datasets.penguins")# Returns a Pandas series with dry run statsdf.to_pandas(dry_run=True)
importbigframes.pandasasbpdsql="""SELECT species, island, body_mass_gFROM bigquery-public-data.ml_datasets.penguinsWHERE sex = 'MALE'"""df=bpd.read_gbq(sql)
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[],[],null,["# Manage BigQuery DataFrames sessions and I/O\n===========================================\n\nThis document explains how to manage sessions and perform input and output (I/O)\noperations when you use BigQuery DataFrames. You will learn how to create and\nuse sessions, work with in-memory data, and read from and write to files and\nBigQuery tables.\n\nBigQuery sessions\n-----------------\n\nBigQuery DataFrames uses a local session object internally to manage\nmetadata. Each `DataFrame` and `Series` object connects to a session, each\nsession connects to a [location](/bigquery/docs/locations), and each query in a\nsession runs in the location where you created the session. Use the following\ncode sample to manually create a session and use it for loading data: \n\n import https://cloud.google.com/python/docs/reference/bigframes/latest/\n import bigframes.pandas as bpd\n\n # Create session object\n context = https://cloud.google.com/python/docs/reference/bigframes/latest/.https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes._config.bigquery_options.BigQueryOptions.html(\n project=YOUR_PROJECT_ID,\n location=YOUR_LOCATION,\n )\n session = https://cloud.google.com/python/docs/reference/bigframes/latest/.Session(context)\n\n # Load a BigQuery table into a dataframe\n df1 = https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.operations.blob.BlobAccessor.html#bigframes_operations_blob_BlobAccessor_session.https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.pandas.html(\"bigquery-public-data.ml_datasets.penguins\")\n\n # Create a dataframe with local data:\n df2 = bpd.DataFrame({\"my_col\": [1, 2, 3]}, session=session)\n\nYou can't combine data from multiple session instances, even if you initialize\nthem with the same settings. The following code sample shows that trying to\ncombine data from different session instances causes an error: \n\n import https://cloud.google.com/python/docs/reference/bigframes/latest/\n import bigframes.pandas as bpd\n\n context = https://cloud.google.com/python/docs/reference/bigframes/latest/.https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes._config.bigquery_options.BigQueryOptions.html(location=YOUR_LOCATION, project=YOUR_PROJECT_ID)\n\n session1 = https://cloud.google.com/python/docs/reference/bigframes/latest/.Session(context)\n session2 = https://cloud.google.com/python/docs/reference/bigframes/latest/.Session(context)\n\n series1 = bpd.Series([1, 2, 3, 4, 5], session=session1)\n series2 = bpd.Series([1, 2, 3, 4, 5], session=session2)\n\n try:\n series1 + series2\n except ValueError as e:\n print(e) # Error message: Cannot use combine sources from multiple sessions\n\n### Global session\n\nBigQuery DataFrames provides a default global session that you can\naccess with the `bigframes.pandas.get_global_session()` method. In\nColab, you must provide a project ID for the\n`bigframes.pandas.options.bigquery.project` attribute before you use it. You\ncan also set a location with the\n`bigframes.pandas.options.bigquery.location` attribute, which defaults to\nthe `US` multi-region.\n\nThe following code sample shows how to set options for the global session: \n\n import bigframes.pandas as bpd\n\n # Set project ID for the global session\n bpd.options.bigquery.project = YOUR_PROJECT_ID\n # Update the global default session location\n bpd.options.bigquery.location = YOUR_LOCATION\n\nTo reset the global session's location or project, close the current session by\nrunning the `bigframes.pandas.close_session()` method.\n\nMany BigQuery DataFrames built-in functions use the global session by\ndefault. The following code sample shows how built-in functions use the global\nsession: \n\n # The following two statements are essentially the same\n df = bpd.read_gbq(\"bigquery-public-data.ml_datasets.penguins\")\n df = bpd.get_global_session().read_gbq(\"bigquery-public-data.ml_datasets.penguins\")\n\nIn-memory data\n--------------\n\nYou can create `Dataframes` and `Series` objects with built-in Python or NumPy\ndata structures, similar to how you create objects with pandas. Use the\nfollowing code sample to create an object: \n\n import numpy as np\n\n import bigframes.pandas as bpd\n\n s = bpd.Series([1, 2, 3])\n\n # Create a dataframe with Python dict\n df = bpd.DataFrame(\n {\n \"col_1\": [1, 2, 3],\n \"col_2\": [4, 5, 6],\n }\n )\n\n # Create a series with Numpy\n s = bpd.Series(np.arange(10))\n\nTo convert `pandas` objects to `DataFrames` objects using the `read_pandas()`\nmethod or constructors, use the following code sample: \n\n import numpy as np\n import pandas as pd\n\n import bigframes.pandas as bpd\n\n pd_df = pd.DataFrame(np.random.randn(4, 2))\n\n # Convert Pandas dataframe to BigQuery DataFrame with read_pandas()\n df_1 = bpd.read_pandas(pd_df)\n # Convert Pandas dataframe to BigQuery DataFrame with the dataframe constructor\n df_2 = bpd.DataFrame(pd_df)\n\nTo use the `to_pandas()` method to load BigQuery DataFrames data into\nyour memory, use the following code sample: \n\n import bigframes.pandas as bpd\n\n bf_df = bpd.DataFrame({\"my_col\": [1, 2, 3]})\n # Returns a Pandas Dataframe\n bf_df.to_pandas()\n\n bf_s = bpd.Series([1, 2, 3])\n # Returns a Pandas Series\n bf_s.to_pandas()\n\n### Cost estimation with the `dry_run` parameter\n\nLoading a large amount of data can take a lot of time and resources. To see how\nmuch data is being processed, use the `dry_run=True` parameter in the\n`to_pandas()` call. Use the following code sample to perform a dry run: \n\n import bigframes.pandas as bpd\n\n df = bpd.read_gbq(\"bigquery-public-data.ml_datasets.penguins\")\n\n # Returns a Pandas series with dry run stats\n df.to_pandas(dry_run=True)\n\nRead and write files\n--------------------\n\nYou can read data from compatible files into a BigQuery DataFrames. These\nfiles can be on your local machine or in Cloud Storage. Use the following code\nsample to read data from a CSV file: \n\n import bigframes.pandas as bpd\n\n # Read a CSV file from GCS\n df = bpd.read_csv(\"gs://cloud-samples-data/bigquery/us-states/us-states.csv\")\n\nTo save your BigQuery DataFrames to local files or Cloud Storage files\nusing the `to_csv` method, use the following code sample: \n\n import bigframes.pandas as bpd\n\n df = bpd.DataFrame({\"my_col\": [1, 2, 3]})\n # Write a dataframe to a CSV file in GCS\n df.to_csv(f\"gs://{YOUR_BUCKET}/myfile*.csv\")\n\nRead and write BigQuery tables\n------------------------------\n\nTo create BigQuery DataFrames using BigQuery table\nreferences and the `bigframes.pandas.read_gbq` function, use the following code\nsample: \n\n import bigframes.pandas as bpd\n\n df = bpd.read_gbq(\"bigquery-public-data.ml_datasets.penguins\")\n\nTo use a SQL string with the `read_gbq()` function to read data into\nBigQuery DataFrames, use the following code sample: \n\n import bigframes.pandas as bpd\n\n sql = \"\"\"\n SELECT species, island, body_mass_g\n FROM bigquery-public-data.ml_datasets.penguins\n WHERE sex = 'MALE'\n \"\"\"\n\n df = bpd.read_gbq(sql)\n\n| **Note:** If you specify a table when calling the `read_gbq()`, `read_gbq_table()`, or `read_gbq_query()` function, and you haven't set the `bigframes.pandas.options.bigquery.location` attribute before the function call, then BigQuery DataFrames automatically sets the `bigframes.pandas.options.bigquery.location` attribute to the table's location. For information on how to manually specify the location, see [Global session](#global-session).\n\nTo save your `DataFrame` object to a BigQuery table, use the\n`to_gbq()` method of your `DataFrame` object. The following code sample shows\nhow to do that: \n\n import bigframes.pandas as bpd\n\n df = bpd.DataFrame({\"my_col\": [1, 2, 3]})\n\n df.to_gbq(f\"{YOUR_PROJECT_ID}.{YOUR_DATASET_ID}.{YOUR_TABLE_NAME}\")\n\nWhat's next\n-----------\n\n- Learn how to [use BigQuery DataFrames](/bigquery/docs/use-bigquery-dataframes).\n- Learn how to [work with data types in BigQuery DataFrames](/bigquery/docs/bigquery-dataframes-datatypes).\n- Learn how to [visualize graphs using BigQuery DataFrames](/bigquery/docs/dataframes-visualizations).\n- Explore the [BigQuery DataFrames API reference](/python/docs/reference/bigframes/latest/summary_overview)."]]