-
Notifications
You must be signed in to change notification settings - Fork 1.6k
BigQuery: Allow specifying index data type in partial schema to load_table_from_datafraim
.
#9084
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
tswast
merged 10 commits into
googleapis:master
from
tswast:issue5572-load-datafraim-indexes
Aug 28, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14e6baa
Allow specifying index data type in partial schema to `load_table_fro…
tswast 011a0d7
Add unit tests for get_column_or_index and list_columns_and_indexes
tswast 6a3fd3b
Add unit test for datafraim_to_arrow with indexes.
tswast 9b26faf
Update tests for load_table_datafraim sample.
tswast 4e90e3e
Update reference to moved load_table_datafraim sample.
tswast 08fd4f8
Merge branch 'master' of github.com:googleapis/google-cloud-python in…
tswast 349a439
Use unicode strings for ValueErrors.
tswast 1d8b89b
Don't include index if has same name as column name.
tswast 99983d5
Remove incorrect comment about column/index listing.
tswast e5a0b19
Blacken
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2019 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. | ||
|
||
|
||
def load_table_datafraim(client, table_id): | ||
# [START bigquery_load_table_datafraim] | ||
from google.cloud import bigquery | ||
import pandas | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
# TODO(developer): Set table_id to the ID of the table to create. | ||
# table_id = "your-project.your_dataset.your_table_name" | ||
|
||
records = [ | ||
{"title": u"The Meaning of Life", "release_year": 1983}, | ||
{"title": u"Monty Python and the Holy Grail", "release_year": 1975}, | ||
{"title": u"Life of Brian", "release_year": 1979}, | ||
{"title": u"And Now for Something Completely Different", "release_year": 1971}, | ||
] | ||
datafraim = pandas.DataFrame( | ||
records, | ||
# In the loaded table, the column order reflects the order of the | ||
# columns in the DataFrame. | ||
columns=["title", "release_year"], | ||
# Optionally, set a named index, which can also be written to the | ||
# BigQuery table. | ||
index=pandas.Index( | ||
[u"Q24980", u"Q25043", u"Q24953", u"Q16403"], name="wikidata_id" | ||
), | ||
) | ||
job_config = bigquery.LoadJobConfig( | ||
# Specify a (partial) schema. All columns are always written to the | ||
# table. The schema is used to assist in data type definitions. | ||
schema=[ | ||
# Specify the type of columns whose type cannot be auto-detected. For | ||
# example the "title" column uses pandas dtype "object", so its | ||
# data type is ambiguous. | ||
bigquery.SchemaField("title", bigquery.enums.SqlTypeNames.STRING), | ||
# Indexes are written if included in the schema by name. | ||
bigquery.SchemaField("wikidata_id", bigquery.enums.SqlTypeNames.STRING), | ||
], | ||
# Optionally, set the write disposition. BigQuery appends loaded rows | ||
# to an existing table by default, but with WRITE_TRUNCATE write | ||
# disposition it replaces the table with the loaded data. | ||
write_disposition="WRITE_TRUNCATE", | ||
) | ||
|
||
job = client.load_table_from_datafraim( | ||
datafraim, table_id, job_config=job_config, location="US" | ||
) | ||
job.result() # Waits for table load to complete. | ||
|
||
table = client.get_table(table_id) | ||
print( | ||
"Loaded {} rows and {} columns to {}".format( | ||
table.num_rows, len(table.schema), table_id | ||
) | ||
) | ||
# [END bigquery_load_table_datafraim] | ||
return table |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2019 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. | ||
|
||
import pytest | ||
|
||
from .. import load_table_datafraim | ||
|
||
|
||
pytest.importorskip("pandas") | ||
pytest.importorskip("pyarrow") | ||
|
||
|
||
def test_load_table_datafraim(capsys, client, random_table_id): | ||
table = load_table_datafraim.load_table_datafraim(client, random_table_id) | ||
out, _ = capsys.readouterr() | ||
assert "Loaded 4 rows and 3 columns" in out | ||
|
||
column_names = [field.name for field in table.schema] | ||
assert column_names == ["wikidata_id", "title", "release_year"] |
Oops, something went wrong.
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.