From 3057b13da9577760e66e374f279252c28c6d1f21 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 5 Dec 2016 11:15:01 -0500 Subject: [PATCH 1/2] Allow timestamp strings in 'Table.insert_data'. Closes #1382. --- bigquery/google/cloud/bigquery/table.py | 9 +++++++-- bigquery/unit_tests/test_table.py | 9 +++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index f8b52f772f87..f9c6a970fa18 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -738,15 +738,20 @@ def insert_data(self, rows_info = [] data = {'rows': rows_info} + def _convert_timestamp(value): + if isinstance(value, datetime.datetime): + value = _microseconds_from_datetime(value) * 1e-6 + return value + for index, row in enumerate(rows): row_info = {} for field, value in zip(self._schema, row): - if field.field_type == 'TIMESTAMP' and value is not None: + if field.field_type == 'TIMESTAMP': # BigQuery stores TIMESTAMP data internally as a # UNIX timestamp with microsecond precision. # Specifies the number of seconds since the epoch. - value = _microseconds_from_datetime(value) * 1e-6 + value = _convert_timestamp(value) row_info[field.name] = value info = {'json': row_info} diff --git a/bigquery/unit_tests/test_table.py b/bigquery/unit_tests/test_table.py index 9fcea12d2dce..0b8453103082 100644 --- a/bigquery/unit_tests/test_table.py +++ b/bigquery/unit_tests/test_table.py @@ -1296,6 +1296,7 @@ def test_insert_data_wo_schema(self): def test_insert_data_w_bound_client(self): import datetime from google.cloud._helpers import UTC + from google.cloud._helpers import _datetime_to_rfc3339 from google.cloud._helpers import _microseconds_from_datetime from google.cloud.bigquery.table import SchemaField @@ -1313,16 +1314,16 @@ def test_insert_data_w_bound_client(self): table = self._make_one(self.TABLE_NAME, dataset=dataset, schema=[full_name, age, joined]) ROWS = [ - ('Phred Phlyntstone', 32, WHEN), + ('Phred Phlyntstone', 32, _datetime_to_rfc3339(WHEN)), ('Bharney Rhubble', 33, WHEN + datetime.timedelta(seconds=1)), ('Wylma Phlyntstone', 29, WHEN + datetime.timedelta(seconds=2)), ('Bhettye Rhubble', 27, None), ] def _row_data(row): - joined = None - if row[2] is not None: - joined = _microseconds_from_datetime(row[2]) * 1e-6 + joined = row[2] + if isinstance(row[2], datetime.datetime): + joined = _microseconds_from_datetime(joined) * 1e-6 return {'full_name': row[0], 'age': row[1], 'joined': joined} From 93db7dafb5a6edf6486023f1d973738247f7b49f Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 5 Dec 2016 12:52:59 -0500 Subject: [PATCH 2/2] Move '_convert_timestamp' helper out to module scope. Addresses: https://github.com/GoogleCloudPlatform/google-cloud-python/pull/2805#discussion_r90912170 --- bigquery/google/cloud/bigquery/table.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index f9c6a970fa18..18f83c596f7d 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -738,11 +738,6 @@ def insert_data(self, rows_info = [] data = {'rows': rows_info} - def _convert_timestamp(value): - if isinstance(value, datetime.datetime): - value = _microseconds_from_datetime(value) * 1e-6 - return value - for index, row in enumerate(rows): row_info = {} @@ -1134,3 +1129,10 @@ class _UrlBuilder(object): def __init__(self): self.query_params = {} self._relative_path = '' + + +def _convert_timestamp(value): + """Helper for :meth:`Table.insert_data`.""" + if isinstance(value, datetime.datetime): + value = _microseconds_from_datetime(value) * 1e-6 + return value