首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pandas.to_gbq()返回"ArrowTypeError:预期字节,得到一个'datetime.date‘对象’错误

问pandas.to_gbq()返回"ArrowTypeError:预期字节,得到一个'datetime.date‘对象’错误
EN

Stack Overflow用户
提问于 2022-07-08 09:21:14
回答 2查看 2K关注 0票数 3

pandas.to_gbq()最近开始返回一个错误,尽管df模式/数据类型与BigQuery表的数据类型完全相同,但当我试图将数据添加到BigQuery表时,返回了一个错误。

下面的代码片段:

代码语言:javascript
复制
df.to_gbq(destination_table = PROCESSED_DATA_TABLE_NAME,
          project_id = PROJECT_NAME,
          if_exists = 'append')

返回:

代码语言:javascript
复制
  File ~\Documents\DartsModel\update_processed_visit_data\main_dev.py:152 in <module>
    df.to_gbq(destination_table = PROCESSED_DATA_TABLE_NAME,

  File ~\Anaconda3\envs\darts_model\lib\site-packages\pandas\core\frame.py:2054 in to_gbq
    gbq.to_gbq(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\pandas\io\gbq.py:212 in to_gbq
    pandas_gbq.to_gbq(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\pandas_gbq\gbq.py:1198 in to_gbq
    connector.load_data(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\pandas_gbq\gbq.py:591 in load_data
    chunks = load.load_chunks(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\pandas_gbq\load.py:238 in load_chunks
    load_parquet(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\pandas_gbq\load.py:130 in load_parquet
    client.load_table_from_dataframe(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\google\cloud\bigquery\client.py:2628 in load_table_from_dataframe
    _pandas_helpers.dataframe_to_parquet(

  File ~\Anaconda3\envs\darts_model\lib\site-packages\google\cloud\bigquery\_pandas_helpers.py:672 in dataframe_to_parquet
    arrow_table = dataframe_to_arrow(dataframe, bq_schema)

  File ~\Anaconda3\envs\darts_model\lib\site-packages\google\cloud\bigquery\_pandas_helpers.py:617 in dataframe_to_arrow
    bq_to_arrow_array(get_column_or_index(dataframe, bq_field.name), bq_field)

  File ~\Anaconda3\envs\darts_model\lib\site-packages\google\cloud\bigquery\_pandas_helpers.py:342 in bq_to_arrow_array
    return pyarrow.Array.from_pandas(series, type=arrow_type)

  File pyarrow\array.pxi:1033 in pyarrow.lib.Array.from_pandas

  File pyarrow\array.pxi:312 in pyarrow.lib.array

  File pyarrow\array.pxi:83 in pyarrow.lib._ndarray_to_array

  File pyarrow\error.pxi:123 in pyarrow.lib.check_status

ArrowTypeError: Expected bytes, got a 'datetime.date' object

有关的一揽子案文如下:

代码语言:javascript
复制
python==3.9.12
pandas==1.4.2
pandas-gbq==0.17.6
arrow==1.2.2
google-cloud-bigquery==3.2.0
google-cloud-bigquery-storage==2.13.2

似乎找不到任何在线解决方案,所以任何帮助都会非常感谢!谢谢。

EN

回答 2

Stack Overflow用户

发布于 2022-08-16 18:34:42

to_gbq()方法的一个替代解决方案是使用google的bigquery包。

虽然bigquery表和本地df的模式是相同的,但是附加到BigQuery表中可以使用以下代码完成:

代码语言:javascript
复制
from google.cloud import bigquery
import pandas as pd

client = bigquery.Client()

# define project, dataset, and table_name variables
project, dataset, table_name = "project", "dataset", "table_name"
table_id = f"{project}.{dataset}.{table_name}"

job_config = bigquery.job.LoadJobConfig()

# set write_disposition parameter as WRITE_APPEND for appending to table
job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND

job = client.load_table_from_dataframe(df, table_id, job_config=job_config)

job.result()  # Wait for the job to complete.

table = client.get_table(table_id)  # Make an API request.
print(
    f"Loaded {table.num_rows} rows and {len(table.schema)} columns to {table_id}"
)

最终结果产生相同的结果--附加到BigQuery表中。

票数 2
EN

Stack Overflow用户

发布于 2022-09-27 09:48:48

您的一个列可能具有“object”类型,但基础类型实际上是一个日期时间。您可以按如下方式转换它,以使to_gbq()工作。但这取决于目标类型是“时间戳”。

代码语言:javascript
复制
object_cols = df.select_dtypes(include=['object']).columns

for column in object_cols:
    dtype = str(type(df[column].values[0]))
    if dtype == "<class 'datetime.date'>":
        df[column]  = pd.to_datetime(df[column] , infer_datetime_format=True)

df.to_gbq(destination_table=PROCESSED_DATA_TABLE_NAME, if_exists='replace')

注意,这将替换表.,如果要追加,则需要通过在to_gbq()函数中使用table_schema参数来确保类型匹配。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72909275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档