没有名为 'db_dtypes' 的模块。

9

运行一个小的Python代码,从Bigquery表格结果中创建pandas dataframe。当我运行代码时,我看到以下结果。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

1
您只包含了部分代码。请包含足够的代码以提供问题的最小可重现性。您说 db_dtypes 已安装(如何安装?)。您是否还在代码中包含了 import db_dtypes - DazWilkin
1
请勿在可复制粘贴文本的情况下包含图片。图片无法搜索、复制粘贴,且可能与问题的生命周期不同。 - DazWilkin
3
你尝试过运行pip install db-dtypes吗?https://pypi.org/project/db-dtypes/ 此外,如@DazWilkin所述,包含足够的代码和文本日志等信息是帮助社区识别和复制您的问题的好方法。 - Nestor Ceniza Jr
将整个代码添加到原始帖子中。 - Pagam
1
你修好了吗?我现在也遇到了同样的问题。 - Fran Sevillano
显示剩余3条评论
2个回答

1
将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)

对于我使用这个库来说,我可以将响应转换成数据框,但是当将数据框转换成pickle格式并重新导入到一个没有pandas_gbq的环境中时,会产生同样的错误...
我猜测一些元数据可能无法被原始的pandas模块正确显示。
编辑:通过查看评论,我们可以发现错误可以通过简单执行 "pip install db-dtypes" 来修复,就像 @DazWilkin 和 @Nestor Ceniza Jr 建议的那样(如果您正在使用jupyter笔记本,则还需要重启内核,如 @dss 所做)。

0
在运行了pip install db-dtypes之后,它对我起作用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接