【发布时间】:2021-10-04 02:00:41
【问题描述】:
给定以下代码:
try:
dest_table = bigquery.table.Table(table_id, schema)
job = self.client.load_table_from_dataframe(
df_data, # pd.DataFrame
dest_table,
job_config=bigquery.job.LoadJobConfig(schema=schema)
)
job.result()
except TypeError:
with pd.option_context("display.max_rows", None, "display.max_columns", None, "display.width", None):
LOG.error("Failed to upload dataframe: \n\n%s\n", df_data.to_csv(header=True, index=False, quoting=csv.QUOTE_NONNUMERIC))
LOG.error("\n%s\n", df_data.dtypes)
if schema:
LOG.error(
"schema: \n\n%s",
('[\n ' + ',\n '.join(json.dumps(field) for field in schema) + '\n]\n')
)
LOG.error(f"dest_table_id: {dest_table_id}")
raise
BigQuery 的 Client.load_table_from_dataframe 从 pyarrow 引发:
dags/utils/database/_bigquery.py:257: in load_file_to_table
job = self.client.load_table_from_dataframe(
lib/python3.8/site-packages/google/cloud/bigquery/client.py:2233: in load_table_from_dataframe
_pandas_helpers.dataframe_to_parquet(
lib/python3.8/site-packages/google/cloud/bigquery/_pandas_helpers.py:486: in dataframe_to_parquet
arrow_table = dataframe_to_arrow(dataframe, bq_schema)
lib/python3.8/site-packages/google/cloud/bigquery/_pandas_helpers.py:450: in dataframe_to_arrow
bq_to_arrow_array(get_column_or_index(dataframe, bq_field.name), bq_field)
lib/python3.8/site-packages/google/cloud/bigquery/_pandas_helpers.py:224: in bq_to_arrow_array
return pyarrow.Array.from_pandas(series, type=arrow_type)
pyarrow/array.pxi:859: in pyarrow.lib.Array.from_pandas
???
pyarrow/array.pxi:265: in pyarrow.lib.array
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> ???
E TypeError: an integer is required (got type str)
然而,在使用 ERROR 调试打印进行调查时,提供的 DataFrame 似乎与为表提供的每个字段的预期架构相匹配:
ERROR utils.database._bigquery:_bigquery.py:265 Failed to upload dataframe:
"clinic_key","schedule_template_time_interval_key","schedule_template_key","date_key","schedule_owner_key","schedule_template_schedule_track_key","schedule_content_label_key","start_time_key","end_time_key","priority"
"clitest11111111111111111111111","1","1","2021-01-01","1","1","1","19:00:00","21:00:00",1
"clitest11111111111111111111111","1","1","2021-01-01","1","1","2","20:00:00","20:30:00",2
"clitest11111111111111111111111","1","1","2021-01-01","1","1","3","20:20:00","20:30:00",3
ERROR utils.database._bigquery:_bigquery.py:266
clinic_key object
schedule_template_time_interval_key object
schedule_template_key object
date_key object
schedule_owner_key object
schedule_template_schedule_track_key object
schedule_content_label_key object
start_time_key object
end_time_key object
priority int64
dtype: object
ERROR utils.database._bigquery:_bigquery.py:268 schema:
[
{"name": "clinic_key", "type": "STRING", "mode": "NULLABLE"},
{"name": "schedule_template_time_interval_key", "type": "STRING", "mode": "NULLABLE"},
{"name": "schedule_template_key", "type": "STRING", "mode": "NULLABLE"},
{"name": "date_key", "type": "DATE", "mode": "NULLABLE"},
{"name": "schedule_owner_key", "type": "STRING", "mode": "NULLABLE"},
{"name": "schedule_template_schedule_track_key", "type": "STRING", "mode": "NULLABLE"},
{"name": "schedule_content_label_key", "type": "STRING", "mode": "NULLABLE"},
{"name": "start_time_key", "type": "TIME", "mode": "NULLABLE"},
{"name": "end_time_key", "type": "TIME", "mode": "NULLABLE"},
{"name": "priority", "type": "INT64", "mode": "NULLABLE"}
]
在调用 load_table_from_dataframe 之前,我尝试将 (start_time_key, end_time_key) 字段转换为 INT(自一天开始以来的秒数),但这并没有解决问题。除此之外,我很困惑;我不明白哪个字段应该是整数,而是字符串。
我该如何解决这个问题?
P.S.:我尝试了一种不同的方法,使用 load_file_to_table,但我遇到了另一个问题。这是link to the other issue。
【问题讨论】:
标签: python pandas google-bigquery