Python Tools for BigQuery
Why?
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.
What?
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.
Install
pip install --upgrade bqtools
Examples:
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:
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
# geo and array are currently not/not fully supported
# data = columns of lists
table = bqtools.BQTable(
schema=schema,
data=[[1, 2, 3, 4], ['a', 'b', 'c', 'd']]
)
# data = rows of dicts
table = bqtools.BQTable(
schema=schema,
data=[
{'number': 1, 'text': 'a'},
{'number': 2, 'text': 'b'},
...
]
)
View data
print(table.data) # 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'}]
table.append(rows)
row = [[6, 'f']]
table.append(rows)
Load table from BigQuery
# requires environment variable GOOGLE_APPLICATION_CREDENTIALS
# or parameter credentials='path-to-credentials.json'
table = bqtools.read_bq(
table_ref='project_id.dataset_id.new_table_id',
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'},
]
table.schema(new_schema)
# 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)
table.save('local_table.bqt')
# load from local file
table = bqtools.load('local_table.bqt')
Metadata
Release files for bqtools 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bqtools-0.6.0.tar.gz | 10.0 kB | Without provenance | Details for bqtools-0.6.0.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bqtools-0.6.0-py3.7.egg | Legacy Egg format | - | - | Details for bqtools-0.6.0-py3.7.egg |
| bqtools-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for bqtools-0.6.0-py3-none-any.whl |
Total release size: 37.1 kB
Release files / bqtools-0.6.0.tar.gz
| Download URL | bqtools-0.6.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8af25c798fa2c6a8338336c3d7f37bd38035fe5aa8bc61671a3c6afb57c84c6f
|
|
BLAKE2b-256 checksum How to use checksums |
785653d2e7beca6d6223e5fd52dcdd095341b66ca6bb1ebe944d6502baf27c21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.8
|
Release files / bqtools-0.6.0-py3.7.egg
| Download URL | bqtools-0.6.0-py3.7.egg |
|---|---|
| Size | 17.9 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
5fa43bc8c48fb6440e05219fc8b83853dab6ab30e2be94c43984b2ae6b3e0ce2
|
|
BLAKE2b-256 checksum How to use checksums |
d12a76eb23c64a4ceb94ccb4037631a2e2b92bdff6043d4ba2bb308ba5a73ba8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.8
|
Release files / bqtools-0.6.0-py3-none-any.whl
| Download URL | bqtools-0.6.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2fa877a0eb58b89d7eb75be5fc4ae27b206b2cded4421d6099f330917dcf34ce
|
|
BLAKE2b-256 checksum How to use checksums |
9cd67cb4fa73cfbae8bb0266941cd11d88857511215a46e691bb6af21aa3071e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.8
|
Security for bqtools 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.