You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pandas-profiling触发TypeCheckError,请求技术排查方案

解决pandas-profiling初始化时的TypeCheckError问题

问题重现

代码

import pandas as pd
from pandas_profiling import ProfileReport
df = pd.read_csv("data.csv")
profile = ProfileReport(df)
profile

报错信息

---------------------------------------------------------------------------
TypeCheckError                            Traceback (most recent call last)
Cell In[18], line 1
----> 1 profile = ProfileReport(df)
      2 profile

File ~\AppData\Local\anaconda3\lib\site-packages\pandas_profiling\profile_report.py:48, in ProfileReport.__init__(self, df, minimal, explorative, sensitive, dark_mode, orange_mode, tsmode, sortby, sample, config_file, lazy, typeset, summarizer, config, **kwargs)
     45 _json = None
     46 config: Settings
---> 48 def __init__(
     49     self,
     50     df: Optional[pd.DataFrame] = None,
     51     minimal: bool = False,
     52     explorative: bool = False,
     53     sensitive: bool = False,
     54     dark_mode: bool = False,
     55     orange_mode: bool = False,
     56     tsmode: bool = False,
     57     sortby: Optional[str] = None,
     58     sample: Optional[dict] = None,
     59     config_file: Union[Path, str] = None,
     60     lazy: bool = True,
     61     typeset: Optional[VisionsTypeset] = None,
     62     summarizer: Optional[BaseSummarizer] = None,
     63     config: Optional[Settings] = None,
     64     **kwargs,
     65 ):
     66     """Generate a ProfileReport based on a pandas DataFrame
     67 
     68     Config processing order (in case of duplicate entries, entries later in the order are retained):
   (...)
     82         **kwargs: other arguments, for valid arguments, check the default configuration file.
     83     """
     85     if df is None and not lazy:

File ~\AppData\Local\anaconda3\lib\site-packages\typeguard\_functions.py:138, in check_argument_types(func_name, arguments, memo)
    135         raise exc
    137 try:
---> 138     check_type_internal(value, annotation, memo)
    139 except TypeCheckError as exc:
    140     qualname = qualified_name(value, add_class_prefix=True)

File ~\AppData\Local\anaconda3\lib\site-packages\typeguard\_checkers.py:759, in check_type_internal(value, annotation, memo)
    757     checker = lookup_func(origin_type, args, extras)
    758     if checker:
---> 759         checker(value, origin_type, args, memo)
    760         return
    762 if isclass(origin_type):

File ~\AppData\Local\anaconda3\lib\site-packages\typeguard\_checkers.py:408, in check_union(value, origin_type, args, memo)
    403         errors[get_type_name(type_)] = exc
    405 formatted_errors = indent(
    406     "\n".join(f"{key}: {error}" for key, error in errors.items()), "  "
    407 )
---> 408 raise TypeCheckError(f"did not match any element in the union:\n{formatted_errors}")

TypeCheckError: argument "config_file" (None) did not match any element in the union:
  pathlib.Path: is not an instance of pathlib.Path
  str: is not an instance of str

依赖版本

  • pandas==1.5.3
  • pandas-profiling==3.6.6

解决方案

方法1:跳过类型检查

显式传入config_file=None并添加类型忽略注释,绕过typeguard的严格检查:

import pandas as pd
from pandas_profiling import ProfileReport

df = pd.read_csv("data.csv")
profile = ProfileReport(df, config_file=None)  # type: ignore[arg-type]
profile

方法2:降级typeguard版本

问题源于高版本typeguard(>=3.0)对Union类型参数的None值检查过于严格,而pandas-profiling 3.6.6的类型注解未将config_file标记为可选类型。降级到兼容版本:

pip install typeguard==2.13.3

方法3:更换为最新稳定版工具

pandas-profiling已更名为ydata-profiling,更新到最新版可解决此类类型注解问题:

pip uninstall pandas-profiling
pip install ydata-profiling

修改导入代码后重新运行:

import pandas as pd
from ydata_profiling import ProfileReport

df = pd.read_csv("data.csv")
profile = ProfileReport(df)
profile

问题原因

pandas-profiling==3.6.6中ProfileReport.__init__的config_file参数类型注解为Union[Path, str],但默认值设为None,而高版本typeguard会严格校验Union类型参数必须属于注解中的某一种类型,不允许传入None,因此触发TypeCheckError。

内容的提问来源于stack exchange,提问作者Senthil Vikram Vodapalli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 13:35:55