Skip to content

Commit 1458da6

Browse files
committed
adds test for pandas handling of basic BigQuery data types
1 parent 1d58a73 commit 1458da6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

bigquery/tests/unit/test_table.py

+46
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,52 @@ def test_to_dataframe_w_empty_results(self):
895895
self.assertEqual(len(df), 0) # verify the number of rows
896896
self.assertEqual(list(df), ['name', 'age']) # verify the column names
897897

898+
@unittest.skipIf(pandas is None, 'Requires `pandas`')
899+
def test_to_dataframe_w_various_types(self):
900+
import datetime
901+
from google.cloud.bigquery.table import RowIterator
902+
from google.cloud.bigquery.table import SchemaField
903+
from google.cloud.bigquery._helpers import _field_to_index_mapping
904+
905+
schema = [
906+
SchemaField('start_timestamp', 'TIMESTAMP'),
907+
SchemaField('seconds', 'INT64'),
908+
SchemaField('miles', 'FLOAT64'),
909+
SchemaField('payment_type', 'STRING'),
910+
SchemaField('complete', 'BOOL'),
911+
SchemaField('date', 'DATE'),
912+
]
913+
row_data = [
914+
[None, None, None, None, None, None],
915+
['1.4338368E9', '420', '1.1', 'Cash', 'true', '1999-12-01'],
916+
['1.3878117E9', '2580', '17.7', 'Cash', 'false', '1953-06-14'],
917+
['1.3855653E9', '2280', '4.4', 'Credit', 'true', '1981-11-04'],
918+
]
919+
rows = [{'f': [{'v': field} for field in row]} for row in row_data]
920+
path = '/foo'
921+
api_request = mock.Mock(return_value={'rows': rows})
922+
row_iterator = RowIterator(
923+
mock.sentinel.client, api_request, path=path)
924+
row_iterator._schema = schema
925+
row_iterator._field_to_index = _field_to_index_mapping(schema)
926+
927+
df = row_iterator.to_dataframe()
928+
929+
self.assertIsInstance(df, pandas.DataFrame)
930+
self.assertEqual(len(df), 4) # verify the number of rows
931+
exp_columns = [field.name for field in schema]
932+
self.assertEqual(list(df), exp_columns) # verify the column names
933+
934+
for index, row in df.iterrows():
935+
if index == 0:
936+
self.assertTrue(row.isnull().all())
937+
else:
938+
self.assertIsInstance(row.start_timestamp, pandas.Timestamp)
939+
self.assertIsInstance(row.seconds, float)
940+
self.assertIsInstance(row.payment_type, str)
941+
self.assertIsInstance(row.complete, bool)
942+
self.assertIsInstance(row.date, datetime.date)
943+
898944
@mock.patch('google.cloud.bigquery.table.pandas', new=None)
899945
def test_to_dataframe_error_if_pandas_is_none(self):
900946
from google.cloud.bigquery.table import RowIterator

0 commit comments

Comments
 (0)