简体   繁体   English

将 bigquery 中的所有列转换为浮点数/字符串

[英]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我正在尝试使用 python 下载 BQ 表,如下所示: 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.看起来, google-cloud-sdk首先将表转换为 JSON 格式,然后转储到 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?有没有办法在使用 Python 下载之前将 BQ 中的所有列转换为浮点/字符串?
  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 ).假设您的表有 3 列( abc )。 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
"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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