Skip to content

Commit c7341ee

Browse files
committed
extend to_gbq docstring and add tests for table_schema parameter
1 parent 0ebe1e2 commit c7341ee

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pandas_gbq/gbq.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ def to_gbq(dataframe, destination_table, project_id, chunksize=10000,
816816
Service account private key in JSON format. Can be file path
817817
or string contents. This is useful for remote server
818818
authentication (eg. jupyter iPython notebook on remote host)
819+
table_schema : list of dicts
820+
List of BigQuery table fields to which according DataFrame columns
821+
conform to, e.g. `[{'name': 'col1', 'type': 'STRING'},...]`. If
822+
schema is not provided, it will be generated according to dtypes
823+
of DataFrame columns.
819824
"""
820825

821826
if if_exists not in ('fail', 'replace', 'append'):

pandas_gbq/tests/test_gbq.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,31 @@ def test_verify_schema_ignores_field_mode(self):
12581258
assert self.sut.verify_schema(
12591259
self.dataset_prefix + "1", TABLE_ID + test_id, test_schema_2)
12601260

1261+
def test_upload_data_with_valid_user_schema(self):
1262+
df = tm.makeMixedDataFrame()
1263+
test_id = "15"
1264+
test_schema = [{'name': 'A', 'type': 'FLOAT'},
1265+
{'name': 'B', 'type': 'FLOAT'},
1266+
{'name': 'C', 'type': 'STRING'},
1267+
{'name': 'D', 'type': 'TIMESTAMP'}]
1268+
destination_table = self.destination_table + test_id
1269+
gbq.to_gbq(df, destination_table, _get_project_id(),
1270+
private_key=_get_private_key_path(), table_schema=test_schema)
1271+
dataset, table = destination_table.split('.')
1272+
assert self.table.verify_schema(dataset, table, dict(fields=test_schema))
1273+
1274+
def test_upload_data_with_invalid_user_schema_raises_error(self):
1275+
df = tm.makeMixedDataFrame()
1276+
test_id = "16"
1277+
test_schema = [{'name': 'A', 'type': 'FLOAT'},
1278+
{'name': 'B', 'type': 'FLOAT'},
1279+
{'name': 'C', 'type': 'FLOAT'},
1280+
{'name': 'D', 'type': 'FLOAT'}]
1281+
destination_table = self.destination_table + test_id
1282+
with tm.assertRaises(gbq.StreamingInsertError):
1283+
gbq.to_gbq(df, destination_table, _get_project_id(),
1284+
private_key=_get_private_key_path(), table_schema=test_schema)
1285+
12611286
def test_list_dataset(self):
12621287
dataset_id = self.dataset_prefix + "1"
12631288
assert dataset_id in self.dataset.datasets()

0 commit comments

Comments
 (0)