Skip to main content

Python Tools for BigQuery

Project description

Build Status PyPI version

Python Tools for BigQuery


For data collection and data exploration, we like to work with BigQuery. But we have not found a python library, to easily handle recurring tasks like adding new data (of potentially inconsistent schema) and schema migrations. So we took a couple of our solutions for those tasks and put them into this library.


bqtools provides a light-weight solution to explicit schema management with python-native types (unlike pandas dtype) and some convenient type checking, inference and conversions. Table-objects created by bqtools can be read from BigQuery, stored locally, read from a local file and written to BigQuery. Table schemas can be changed and data can be added or modified.


pip install --upgrade bqtools


Create basic tables

from fourtytwo import bqtools

schema = [
    {'name': 'number', 'field_type': 'INTEGER'},
    {'name': 'text', 'field_type': 'STRING'},
    {'name': 'struct', 'field_type':'RECORD', 'mode':'REPEATED', 
        'fields': [
            {'name':'integer', 'field_type':'INTEGER'},
            {'name':'text', 'field_type':'STRING'}
# valid BigQuery types see: 
# geo and array are currently not/not fully supported

# data = columns of lists
table = bqtools.BQTable(
    data=[[1, 2, 3, 4], ['a', 'b', 'c', 'd']]

# data = rows of dicts
table = bqtools.BQTable(
        {'number': 1, 'text': 'a'}, 
        {'number': 2, 'text': 'b'},

View data

print(       # list of all columns
print(table.rows(n=10)) # list of first n rows

# convert to pandas.DataFrame
df = table.to_df()               
# warning: pandas dtypes may be inconsistent 
# with BigQuery Schema field_types

Append data

rows = [{'number': 5, 'text': 'e'}]

row = [[6, 'f']]

Load table from BigQuery

# requires environment variable GOOGLE_APPLICATION_CREDENTIALS 
# or parameter credentials='path-to-credentials.json'
table = bqtools.read_bq(
    limit=10,           # limit query rows
    schema_only=False   # set True to only add data

Modify table schema

# change column order and field_type
new_schema = [
    {'name': 'text', 'field_type': 'STRING'},
    {'name': 'number', 'field_type': 'FLOAT'},

# change column names
table.rename(columns={'number': 'decimal'})

Write table to BigQuery

# requires environment variable GOOGLE_APPLICATION_CREDENTIALS
# or parameter credentials='path-to-credentials.json'
table.to_bq(table_ref, mode='append')

Persist tables locally

# write to local file (compressed binary format)'local_table.bqt')

# load from local file
table = bqtools.load('local_table.bqt')

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for bqtools, version 0.6.0
Filename, size File type Python version Upload date Hashes
Filename, size bqtools-0.6.0-py3.7.egg (17.9 kB) File type Egg Python version 3.7 Upload date Hashes View
Filename, size bqtools-0.6.0-py3-none-any.whl (9.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size bqtools-0.6.0.tar.gz (10.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page