Skip to content

Commit 109d47b

Browse files
author
Matti Remes
committed
BUG: Add support to replace partitions in date-partitioned tables (googleapis#47)
1 parent 99cd849 commit 109d47b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pandas_gbq/gbq.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,9 @@ def delete_and_recreate_table(self, dataset_id, table_id, table_schema):
699699
table = _Table(self.project_id, dataset_id,
700700
private_key=self.private_key)
701701
table.delete(table_id)
702-
table.create(table_id, table_schema)
703-
sleep(delay)
702+
if _Table.partition_decorator not in table_id:
703+
table.create(table_id, table_schema)
704+
sleep(delay)
704705

705706

706707
def _get_credentials_file():
@@ -1007,6 +1008,8 @@ def _generate_bq_schema(df, default_type='STRING'):
10071008

10081009
class _Table(GbqConnector):
10091010

1011+
partition_decorator = '$'
1012+
10101013
def __init__(self, project_id, dataset_id, reauth=False, verbose=False,
10111014
private_key=None):
10121015
self.dataset_id = dataset_id
@@ -1036,7 +1039,7 @@ def exists(self, table_id):
10361039
except self.http_error as ex:
10371040
self.process_http_error(ex)
10381041

1039-
def create(self, table_id, schema):
1042+
def create(self, table_id, schema, date_partitioned=False):
10401043
""" Create a table in Google BigQuery given a table and schema
10411044
10421045
Parameters
@@ -1046,6 +1049,8 @@ def create(self, table_id, schema):
10461049
schema : str
10471050
Use the generate_bq_schema to generate your table schema from a
10481051
dataframe.
1052+
date_partitioned: boolean
1053+
Whether table is to be created as a date partitioned table.
10491054
"""
10501055
from google.cloud.bigquery import SchemaField
10511056
from google.cloud.bigquery import Table
@@ -1062,6 +1067,9 @@ def create(self, table_id, schema):
10621067
table_ref = self.client.dataset(self.dataset_id).table(table_id)
10631068
table = Table(table_ref)
10641069

1070+
if date_partitioned or '$' in table_id:
1071+
table.partitioning_type = 'DAY'
1072+
10651073
# Manually create the schema objects, adding NULLABLE mode
10661074
# as a workaround for
10671075
# https://github.com/GoogleCloudPlatform/google-cloud-python/issues/4456

0 commit comments

Comments
 (0)