I'm loading data into Google's BigQuery using the google-cloud-python library as found here:
http://google-cloud-python.readthedocs.io/en/latest/index.html
Part of it involves checking the data types of the data I'm importing to ensure they match the datatypes in the schema for the target table. To do this I convert a python object type to the GBQ equivalent:
import datetime
def convert_type_to_bigquery(object_type):
if isinstance(object_type, str):
return 'STRING'
elif isinstance(object_type, bytes):
return 'BYTES'
elif isinstance(object_type, int):
return 'INTEGER'
elif isinstance(object_type, float):
return 'FLOAT'
elif isinstance(object_type, bool):
return 'BOOLEAN'
elif isinstance(object_type, datetime.date):
return 'DATE'
elif isinstance(object_type, datetime.time):
return 'TIME'
elif isinstance(object_type, datetime.datetime):
return 'DATETIME'
elif isinstance(object_type, ???):
return 'TIMESTAMP'
else:
return None
I can find equivalents for all but the TIMESTAMP type. Is there an equivalent I can use in isinstance()?
You can do print(type(object_type)) with the TIMESTAMP object to be sure but, according to this github page, it looks like it's just datetime.datetime:
...
Date = datetime.date
Time = datetime.time
Timestamp = datetime.datetime
...
I must say, the main reason I wanted to add an answer was to point out a common error with ìsinstance(...). This won't work since bool is an instance of int:
elif isinstance(object_type, int):
return 'INTEGER'
elif isinstance(object_type, float):
return 'FLOAT'
elif isinstance(object_type, bool):
return 'BOOLEAN'
Look at this example:
>>> isinstance(True, int)
True
You need to use elif type(object_type) is int: or place the check for bool first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With