Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(bigquery): allow passing schema as a sequence of dicts#9550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
80a34b4
feat(bigquery): add _to_schema_fields() schema helper
plamut b05887d
Allow passing schema as dicts in table.py
plamut 98b90a6
Allow passing schema as dicts in job.py
plamut 37db6c2
Import SchemaField directly in several tests
plamut 323573e
Allow passing schema as dicts _helpers
plamut 65301f7
Allow passing schema as dicts in pandas helpers
plamut 004c35a
Replace return statement with an else block
plamut 7657e3d
Alter the type spec of values in schema field dict
plamut c45ec71
Blacken a few files
plamut 3d2ce6e
Simplify _to_schema_fields() schema helper
plamut 7007272
Update docstrings for schema parameter
plamut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -239,7 +239,10 @@ def dataframe_to_bq_schema(dataframe, bq_schema): | ||
| Args: | ||
| dataframe (pandas.DataFrame): | ||
| DataFrame for which the client determines the BigQuery schema. | ||
| bq_schema (Sequence[google.cloud.bigquery.schema.SchemaField]): | ||
| bq_schema (Sequence[Union[ \ | ||
| :class:`~google.cloud.bigquery.schema.SchemaField`, \ | ||
| Mapping[str, Any] \ | ||
| ]]): | ||
| A BigQuery schema. Use this argument to override the autodetected | ||
| type for some or all of the DataFrame columns. | ||
| @@ -249,6 +252,7 @@ def dataframe_to_bq_schema(dataframe, bq_schema): | ||
| any column cannot be determined. | ||
| """ | ||
| if bq_schema: | ||
| bq_schema = schema._to_schema_fields(bq_schema) | ||
| for field in bq_schema: | ||
| if field.field_type in schema._STRUCT_TYPES: | ||
| raise ValueError( | ||
| @@ -297,9 +301,12 @@ def dataframe_to_arrow(dataframe, bq_schema): | ||
| Args: | ||
| dataframe (pandas.DataFrame): | ||
| DataFrame to convert to Arrow table. | ||
| bq_schema (Sequence[google.cloud.bigquery.schema.SchemaField]): | ||
| Desired BigQuery schema. Number of columns must match number of | ||
| columns in the DataFrame. | ||
| bq_schema (Sequence[Union[ \ | ||
| :class:`~google.cloud.bigquery.schema.SchemaField`, \ | ||
| Mapping[str, Any] \ | ||
| ]]): | ||
| Desired BigQuery schema. The number of columns must match the | ||
| number of columns in the DataFrame. | ||
| Returns: | ||
| pyarrow.Table: | ||
| @@ -310,6 +317,8 @@ def dataframe_to_arrow(dataframe, bq_schema): | ||
| column_and_index_names = set( | ||
| name for name, _ in list_columns_and_indexes(dataframe) | ||
| ) | ||
| bq_schema = schema._to_schema_fields(bq_schema) | ||
| bq_field_names = set(field.name for field in bq_schema) | ||
| extra_fields = bq_field_names - column_and_index_names | ||
| @@ -354,7 +363,10 @@ def dataframe_to_parquet(dataframe, bq_schema, filepath, parquet_compression="SN | ||
| Args: | ||
| dataframe (pandas.DataFrame): | ||
| DataFrame to convert to Parquet file. | ||
| bq_schema (Sequence[google.cloud.bigquery.schema.SchemaField]): | ||
| bq_schema (Sequence[Union[ \ | ||
| :class:`~google.cloud.bigquery.schema.SchemaField`, \ | ||
| Mapping[str, Any] \ | ||
| ]]): | ||
| Desired BigQuery schema. Number of columns must match number of | ||
| columns in the DataFrame. | ||
| filepath (str): | ||
| @@ -368,6 +380,7 @@ def dataframe_to_parquet(dataframe, bq_schema, filepath, parquet_compression="SN | ||
| if pyarrow is None: | ||
| raise ValueError("pyarrow is required for BigQuery schema conversion.") | ||
| bq_schema = schema._to_schema_fields(bq_schema) | ||
| arrow_table = dataframe_to_arrow(dataframe, bq_schema) | ||
| pyarrow.parquet.write_table(arrow_table, filepath, compression=parquet_compression) | ||
| @@ -388,20 +401,24 @@ def _tabledata_list_page_to_arrow(page, column_names, arrow_types): | ||
| return pyarrow.RecordBatch.from_arrays(arrays, names=column_names) | ||
| def download_arrow_tabledata_list(pages, schema): | ||
| def download_arrow_tabledata_list(pages, bq_schema): | ||
| """Use tabledata.list to construct an iterable of RecordBatches. | ||
| Args: | ||
| pages (Iterator[:class:`google.api_core.page_iterator.Page`]): | ||
| An iterator over the result pages. | ||
| schema (Sequence[google.cloud.bigquery.schema.SchemaField]): | ||
| bq_schema (Sequence[Union[ \ | ||
| :class:`~google.cloud.bigquery.schema.SchemaField`, \ | ||
| Mapping[str, Any] \ | ||
| ]]): | ||
| A decription of the fields in result pages. | ||
| Yields: | ||
| :class:`pyarrow.RecordBatch` | ||
| The next page of records as a ``pyarrow`` record batch. | ||
| """ | ||
| column_names = bq_to_arrow_schema(schema) or [field.name for field in schema] | ||
| arrow_types = [bq_to_arrow_data_type(field) for field in schema] | ||
| bq_schema = schema._to_schema_fields(bq_schema) | ||
| column_names = bq_to_arrow_schema(bq_schema) or [field.name for field in bq_schema] | ||
| arrow_types = [bq_to_arrow_data_type(field) for field in bq_schema] | ||
| for page in pages: | ||
| yield _tabledata_list_page_to_arrow(page, column_names, arrow_types) | ||
| @@ -422,9 +439,26 @@ def _tabledata_list_page_to_dataframe(page, column_names, dtypes): | ||
| return pandas.DataFrame(columns, columns=column_names) | ||
| def download_dataframe_tabledata_list(pages, schema, dtypes): | ||
| """Use (slower, but free) tabledata.list to construct a DataFrame.""" | ||
| column_names = [field.name for field in schema] | ||
| def download_dataframe_tabledata_list(pages, bq_schema, dtypes): | ||
plamut marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """Use (slower, but free) tabledata.list to construct a DataFrame. | ||
| Args: | ||
| pages (Iterator[:class:`google.api_core.page_iterator.Page`]): | ||
| An iterator over the result pages. | ||
| bq_schema (Sequence[Union[ \ | ||
| :class:`~google.cloud.bigquery.schema.SchemaField`, \ | ||
| Mapping[str, Any] \ | ||
| ]]): | ||
| A decription of the fields in result pages. | ||
| dtypes(Mapping[str, numpy.dtype]): | ||
| The types of columns in result data to hint construction of the | ||
| resulting DataFrame. Not all column types have to be specified. | ||
| Yields: | ||
| :class:`pandas.DataFrame` | ||
| The next page of records as a ``pandas.DataFrame`` record batch. | ||
| """ | ||
| bq_schema = schema._to_schema_fields(bq_schema) | ||
| column_names = [field.name for field in bq_schema] | ||
| for page in pages: | ||
| yield _tabledata_list_page_to_dataframe(page, column_names, dtypes) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.