Skip to content

Commit 0a1e91c

Browse files
author
Robert Lacok
committed
to_gbq respects location argument properly
If dataset does not exist, it gets created in the correct location
1 parent 8b7f8fd commit 0a1e91c

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

pandas_gbq/gbq.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ def to_gbq(
895895
dataset_id, table_id = destination_table.rsplit(".", 1)
896896

897897
table = _Table(
898-
project_id, dataset_id, reauth=reauth, private_key=private_key
898+
project_id, dataset_id, reauth=reauth, private_key=private_key, location=location
899899
)
900900

901901
if not table_schema:
@@ -967,9 +967,9 @@ def _generate_bq_schema(df, default_type="STRING"):
967967

968968

969969
class _Table(GbqConnector):
970-
def __init__(self, project_id, dataset_id, reauth=False, private_key=None):
970+
def __init__(self, project_id, dataset_id, reauth=False, private_key=None, location=None):
971971
self.dataset_id = dataset_id
972-
super(_Table, self).__init__(project_id, reauth, private_key)
972+
super(_Table, self).__init__(project_id, reauth, private_key, location=location)
973973

974974
def exists(self, table_id):
975975
""" Check if a table exists in Google BigQuery
@@ -1017,7 +1017,7 @@ def create(self, table_id, schema):
10171017
if not _Dataset(self.project_id, private_key=self.private_key).exists(
10181018
self.dataset_id
10191019
):
1020-
_Dataset(self.project_id, private_key=self.private_key).create(
1020+
_Dataset(self.project_id, private_key=self.private_key, location=self.location).create(
10211021
self.dataset_id
10221022
)
10231023

@@ -1064,8 +1064,8 @@ def delete(self, table_id):
10641064

10651065

10661066
class _Dataset(GbqConnector):
1067-
def __init__(self, project_id, reauth=False, private_key=None):
1068-
super(_Dataset, self).__init__(project_id, reauth, private_key)
1067+
def __init__(self, project_id, reauth=False, private_key=None, location=None):
1068+
super(_Dataset, self).__init__(project_id, reauth, private_key, location=location)
10691069

10701070
def exists(self, dataset_id):
10711071
""" Check if a dataset exists in Google BigQuery
@@ -1106,6 +1106,9 @@ def create(self, dataset_id):
11061106
)
11071107

11081108
dataset = Dataset(self.client.dataset(dataset_id))
1109+
1110+
if self.location is not None:
1111+
dataset.location = self.location
11091112

11101113
try:
11111114
self.client.create_dataset(dataset)

tests/system/test_gbq.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,26 @@ def test_upload_data_tokyo(
13251325
)
13261326
assert table.num_rows > 0
13271327

1328+
def test_upload_data_tokyo_non_existing_dataset(self, project_id, random_dataset_id, bigquery_client):
1329+
test_size = 10
1330+
df = make_mixed_dataframe_v2(test_size)
1331+
non_existing_tokyo_dataset = random_dataset_id
1332+
non_existing_tokyo_destination = "{}.to_gbq_test".format(non_existing_tokyo_dataset)
1333+
1334+
# Initialize table with sample data
1335+
gbq.to_gbq(
1336+
df,
1337+
non_existing_tokyo_destination,
1338+
project_id,
1339+
private_key=self.credentials,
1340+
location="asia-northeast1",
1341+
)
1342+
1343+
table = bigquery_client.get_table(
1344+
bigquery_client.dataset(non_existing_tokyo_dataset).table("to_gbq_test")
1345+
)
1346+
assert table.num_rows > 0
1347+
13281348

13291349
# _Dataset tests
13301350

0 commit comments

Comments
 (0)