pandas 没有名为“db_dtypes”的模块

cmssoen2  于 2023-11-15  发布在  其他
关注(0)|答案(2)|浏览(98)

运行一个小的python代码,从Bigquery表结果创建一个pandas框架。当我运行代码时,我看到下面的结果。db_dtypes已经安装,不确定我需要添加什么其他依赖项。感谢任何帮助。
这里是代码

import pandas

from google.cloud import bigquery
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    '/Users/kar/Downloads/data-4045ff698b4f.json')

project_id = 'data-platform'
client = bigquery.Client(credentials=credentials, project=project_id)


sql = """SELECT * FROM `data-platform.airbnb.raw_hosts` LIMIT 1"""
query_job = client.query(sql)
df = query_job.to_dataframe()

字符串
误差

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/ka/PycharmProjects/pythonProject4/main.py", line 17, in <module>
    df = query_job.to_dataframe()
  File "/Users/ka/PycharmProjects/pythonProject4/venv/lib/python3.7/site-packages/google/cloud/bigquery/job/query.py", line 1689, in to_dataframe
    geography_as_object=geography_as_object,
  File "/Users/ka/PycharmProjects/pythonProject4/venv/lib/python3.7/site-packages/google/cloud/bigquery/table.py", line 1965, in to_dataframe
    _pandas_helpers.verify_pandas_imports()
  File "/Users/ka/PycharmProjects/pythonProject4/venv/lib/python3.7/site-packages/google/cloud/bigquery/_pandas_helpers.py", line 991, in verify_pandas_imports
    raise ValueError(_NO_DB_TYPES_ERROR) from db_dtypes_import_exception
ValueError: Please install the 'db-dtypes' package to use this function.

Process finished with exit code 1

6vl6ewon

6vl6ewon1#

将BigQuery表转换成pandas似乎是一件痛苦的事情,有一个pandas方法(和相关的库)可以实现它(参见pandas.read_gcp)。
我建议使用这个模块而不是原生的 bigquery 模块。

import pandas_gbq
... # your code for getting credentials and query-string
df = pandas_gbq.read_gbq(sql, project_id=project_id, credentials=credentials, progress_bar_type=None)

字符串
对于我使用这个库,我可以将响应转换为 Dataframe ,但是当将 Dataframe 转换为 pickle 格式,然后将其重新导入到没有 pandas_gbq 的环境中时,它会产生相同的错误.
我假设vanilla pandas模块不能正确显示某些元数据。

编辑:通过查看评论,可以发现错误可以很容易地通过简单地执行pip install db-dtypes来修复,就像@DazWilkin和@ Nintendo Ceniza Jr建议的那样(如果你像@dss那样使用jupyter笔记本,重新启动内核)

z8dt9xmd

z8dt9xmd2#

运行pip install db-dtypes后,它对我很有效。

相关问题