我正在使用pandas_gbq
模块尝试将 Dataframe 附加到Google BigQuery中的表中。
我一直收到这个错误:
ArrowTypeError:应为字节数,但获得了“int”对象。
我可以确认dataframe的数据类型与BQ表的模式匹配。
我发现这篇文章关于 parquet 文件不能有混合数据类型:Pandas to parquet file
在我收到的错误消息中,我看到有一个对Parquet文件的引用,所以我假设df.to_gbq()
调用正在创建一个Parquet文件,并且我有一个混合数据类型列,这导致了错误。错误消息没有指定。
我认为我的挑战是我无法找到哪个列具有混合数据类型-我尝试将它们全部转换为字符串,然后指定表模式参数,但这也不起作用。
这是完整的错误追溯:
In [76]: df.to_gbq('Pricecrawler.Daily_Crawl_Data', project_id=project_id, if_exists='append')
ArrowTypeError Traceback (most recent call last)
<ipython-input-76-74cec633c5d0> in <module>
----> 1 df.to_gbq('Pricecrawler.Daily_Crawl_Data', project_id=project_id, if_exists='append')
~\Anaconda3\lib\site-packages\pandas\core\frame.py in to_gbq(self, destination_table,
project_id, chunksize, reauth, if_exists, auth_local_webserver, table_schema, location,
progress_bar, credentials)
1708 from pandas.io import gbq
1709
-> 1710 gbq.to_gbq(
1711 self,
1712 destination_table,
~\Anaconda3\lib\site-packages\pandas\io\gbq.py in to_gbq(dataframe, destination_table, project_id, chunksize, reauth, if_exists, auth_local_webserver, table_schema, location, progress_bar, credentials)
209 ) -> None:
210 pandas_gbq = _try_import()
--> 211 pandas_gbq.to_gbq(
212 dataframe,
213 destination_table,
~\Anaconda3\lib\site-packages\pandas_gbq\gbq.py in to_gbq(dataframe, destination_table, project_id, chunksize, reauth, if_exists, auth_local_webserver, table_schema, location, progress_bar, credentials, api_method, verbose, private_key)
1191 return
1192
-> 1193 connector.load_data(
1194 dataframe,
1195 destination_table_ref,
~\Anaconda3\lib\site-packages\pandas_gbq\gbq.py in load_data(self, dataframe, destination_table_ref, chunksize, schema, progress_bar, api_method, billing_project)
584
585 try:
--> 586 chunks = load.load_chunks(
587 self.client,
588 dataframe,
~\Anaconda3\lib\site-packages\pandas_gbq\load.py in load_chunks(client, dataframe, destination_table_ref, chunksize, schema, location, api_method, billing_project)
235 ):
236 if api_method == "load_parquet":
--> 237 load_parquet(
238 client,
239 dataframe,
~\Anaconda3\lib\site-packages\pandas_gbq\load.py in load_parquet(client, dataframe, destination_table_ref, location, schema, billing_project)
127
128 try:
--> 129 client.load_table_from_dataframe(
130 dataframe,
131 destination_table_ref,
~\Anaconda3\lib\site-packages\google\cloud\bigquery\client.py in load_table_from_dataframe(self, dataframe, destination, num_retries, job_id, job_id_prefix, location, project, job_config, parquet_compression, timeout)
2669 parquet_compression = parquet_compression.upper()
2670
-> 2671 _pandas_helpers.dataframe_to_parquet(
2672 dataframe,
2673 job_config.schema,
~\Anaconda3\lib\site-packages\google\cloud\bigquery\_pandas_helpers.py in dataframe_to_parquet(dataframe, bq_schema, filepath, parquet_compression, parquet_use_compliant_nested_type)
584
585 bq_schema = schema._to_schema_fields(bq_schema)
--> 586 arrow_table = dataframe_to_arrow(dataframe, bq_schema)
587 pyarrow.parquet.write_table(
588 arrow_table, filepath, compression=parquet_compression, **kwargs,
~\Anaconda3\lib\site-packages\google\cloud\bigquery\_pandas_helpers.py in dataframe_to_arrow(dataframe, bq_schema)
527 arrow_names.append(bq_field.name)
528 arrow_arrays.append(
--> 529 bq_to_arrow_array(get_column_or_index(dataframe, bq_field.name), bq_field)
530 )
531 arrow_fields.append(bq_to_arrow_field(bq_field, arrow_arrays[-1].type))
~\Anaconda3\lib\site-packages\google\cloud\bigquery\_pandas_helpers.py in bq_to_arrow_array(series, bq_field)
288 if field_type_upper in schema._STRUCT_TYPES:
289 return pyarrow.StructArray.from_pandas(series, type=arrow_type)
--> 290 return pyarrow.Array.from_pandas(series, type=arrow_type)
291
292
~\Anaconda3\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.Array.from_pandas()
~\Anaconda3\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.array()
~\Anaconda3\lib\site-packages\pyarrow\array.pxi in pyarrow.lib._ndarray_to_array()
~\Anaconda3\lib\site-packages\pyarrow\error.pxi in pyarrow.lib.check_status()
ArrowTypeError: Expected bytes, got a 'int' object
4条答案
按热度按时间n3h0vuf21#
有同样的问题-解决它只是
并在上面执行
to_gbq
。nwnhqdif2#
在将API数据加载到BigQuery时,我也遇到了类似的问题,我相信这会更有效地摆脱Int64_field_0。
5us2dqdw3#
这不是一个真正的答案,而是一个笨拙的解决方案。我遇到了与包含INT64类型列的 Dataframe 完全相同的问题。我发现做以下工作:
我不知道为什么会这样。如果你看看
df.info()
和new_df.info()
,两个 Dataframe 似乎是相同的。我决定在将违规的 Dataframe 保存为csv并以该格式上传到biquery后尝试一下,这是可行的。请注意,这特别发生在INT64类型的列上。我上传的 Dataframe 以同样的方式生成,不包含INT64值,没有任何问题。
cngwdvgl4#
以字典的形式提供表的预期模式,如下所示:schema= ['name':'row','type':'INTEGER'},'name':“城市”、“类型”:'STRING'},' name ':'value','type':'INTEGER'}]替换为您的值,然后在函数内部使用table_schema=schema
这个函数允许一个schema被传递. wrong!这个函数需要一个schema被传递,但是如果你不这样做,一个小片段会很快为你自动生成一个schema.当后面的内容与自动生成的schema不匹配时,它会像这样失败,因为默认类型是String,并且在第一列中没有任何东西表明除了默认值之外还需要其他东西.
解决方法alert -〉如果顶部的行填充了所有的字段,它可能会正确地推断出模式是什么,并且您可以避免错误而无需传递模式。
这使得函数的行为看起来很随机,这有时会让人们感到困惑。
第一个解决方案是可行的,因为它使所有内容都是String,并且String是默认的......它可以工作,但是如果你想让所有字段都正确,你现在必须输入一个表。
希望有帮助!如果有,请投票!