gpt4 book ai didi

python - 我在 pandas 上使用 to_gbq 来更新 Google BigQuery 并获得 GenericGBQException

转载 作者:太空狗 更新时间:2023-10-30 01:04:56 30 4
gpt4 key购买 nike

在尝试使用 to_gbq 更新 Google BigQuery 表时,我收到以下响应:

GenericGBQException: Reason: 400 Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1.

我的代码:

gbq.to_gbq(mini_df,'Name-of-Table','Project-id',chunksize=10000,reauth=False,if_exists='append',private_key=None)

我的 mini_df 数据框看起来像:

date    request_number  name    feature_name    value_name  value
2018-01-10 1 1 "a" "b" 0.309457
2018-01-10 1 1 "c" "d" 0.273748

当我运行 to_gbq 时,BigQuery 上没有表,我可以看到该表是使用下一个架构创建的:

日期字符串可空
request_number STRING NULLABLE
名称字符串可空
feature_name STRING NULLABLE
value_name STRING NULLABLE
值 FLOAT NULLABLE

我做错了什么?我该如何解决这个问题?

P.S,其余异常(exception)情况:

BadRequest                                Traceback (most recent call last)
~/anaconda3/envs/env/lib/python3.6/site-packages/pandas_gbq/gbq.py in load_data(self, dataframe, dataset_id, table_id, chunksize)
589 destination_table,
--> 590 job_config=job_config).result()
591 except self.http_error as ex:

~/anaconda3/envs/env/lib/python3.6/site-packages/google/cloud/bigquery/job.py in result(self, timeout)
527 # TODO: modify PollingFuture so it can pass a retry argument to done().
--> 528 return super(_AsyncJob, self).result(timeout=timeout)
529

~/anaconda3/envs/env/lib/python3.6/site-packages/google/api_core/future/polling.py in result(self, timeout)
110 # Pylint doesn't recognize that this is valid in this case.
--> 111 raise self._exception
112

BadRequest: 400 Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1.

During handling of the above exception, another exception occurred:

GenericGBQException Traceback (most recent call last)
<ipython-input-28-195df93249b6> in <module>()
----> 1 gbq.to_gbq(mini_df,'Name-of-Table','Project-id',chunksize=10000,reauth=False,if_exists='append',private_key=None)

~/anaconda3/envs/env/lib/python3.6/site-packages/pandas/io/gbq.py in to_gbq(dataframe, destination_table, project_id, chunksize, verbose, reauth, if_exists, private_key)
106 chunksize=chunksize,
107 verbose=verbose, reauth=reauth,
--> 108 if_exists=if_exists, private_key=private_key)

~/anaconda3/envs/env/lib/python3.6/site-packages/pandas_gbq/gbq.py in to_gbq(dataframe, destination_table, project_id, chunksize, verbose, reauth, if_exists, private_key, auth_local_webserver)
987 table.create(table_id, table_schema)
988
--> 989 connector.load_data(dataframe, dataset_id, table_id, chunksize)
990
991

~/anaconda3/envs/env/lib/python3.6/site-packages/pandas_gbq/gbq.py in load_data(self, dataframe, dataset_id, table_id, chunksize)
590 job_config=job_config).result()
591 except self.http_error as ex:
--> 592 self.process_http_error(ex)
593
594 rows = []

~/anaconda3/envs/env/lib/python3.6/site-packages/pandas_gbq/gbq.py in process_http_error(ex)
454 # <https://cloud.google.com/bigquery/troubleshooting-errors>`__
455
--> 456 raise GenericGBQException("Reason: {0}".format(ex))
457
458 def run_query(self, query, **kwargs):

GenericGBQException: Reason: 400 Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1.

最佳答案

我遇到了同样的问题。

在我的例子中,它取决于数据框的数据类型 object

我有三列 externalIdmappingIdinfo。对于这些字段,我都没有设置数据类型,然后让 pandas 施展魔法。

它决定将所有三个列的数据类型设置为object。问题是,to_gbq 组件在内部使用了 to_json 组件。出于某种原因,如果字段的类型是 object 但仅包含数值,则此输出会省略数据字段周围的引号。

所以 Google Big Query 需要这个

{"externalId": "12345", "mappingId":"abc123", "info":"blerb"}

但是得到了这个:

{"externalId": 12345, "mappingId":"abc123", "info":"blerb"}

并且由于该字段的映射在 Google Big Query 中为 STRING,因此导入过程失败。

提出了两个解决方案。

解决方案 1 - 更改列的数据类型

简单的类型转换有助于解决这个问题。我还必须将 Big Query 中的数据类型更改为 INTEGER

df['externalId'] = df['externalId'].astype('int')

如果是这种情况,Big Query 可以使用不带引号的字段,如 JSON 标准所述。

解决方案 2 - 确保字符串字段是字符串

同样,这是设置数据类型。但由于我们将其显式设置为 String,使用 to_json 的导出会打印出一个带引号的字段并且一切正常。

df['externalId'] = df['externalId'].astype('str')

关于python - 我在 pandas 上使用 to_gbq 来更新 Google BigQuery 并获得 GenericGBQException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191376/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com