简体   繁体   中英

Cast all columns in bigquery to float/string

I'm trying to download a BQ table using python like this: from google.cloud import bigquery

client = bigquery.Client()
SQL_QUERY = """
SELECT *
FROM TABLE
"""
df = client.query(SQL_QUERY).to_dataframe()

I get the following error in the traceback. Looks like, the google-cloud-sdk first converts the table to a JSON format and them dumps to a CSV.

    converter = _CELLDATA_FROM_JSON[field.field_type]
KeyError: 'NUMERIC'

I've two questions:

  1. Is there a way to cast all columns in a BQ to float/string before downloading it using Python?
  2. How do I identify which columns are causing the trouble?

If you distinctively mention your column names, your queries can become more readable.

Lets say your table has 3 columns ( a , b and c ). If you want to cast these as floats, you can simply modify your query to:

SQL_QUERY = """
  SELECT 
    cast(a as float64) as a, 
    cast(b as float64) as b, 
    cast(c as float64) as c
  FROM TABLE
"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM