如何为Pandas dataframe创建Great Expectations checkpoint并解决运行报错
问题解决方法
错误原因
该报错的核心原因是静态YAML配置无法直接引用Python运行时内存中的DataFrame变量:你在Checkpoint配置里写的{df}只会被解析为普通字符串,不会对应到你代码里读取CSV生成的pandas DataFrame对象,因此Great Expectations在校验时无法识别到有效的数据输入,抛出对应错误。
修复方案
方案1:运行Checkpoint时动态传入已构造的BatchRequest
- 先修改Checkpoint的YAML配置,删除带变量的
runtime_parameters配置,简化为通用配置:
name: my_checkpoint_2 config_version: 1 class_name: SimpleCheckpoint expectation_suite_name: My_validations
- 运行Checkpoint时直接传入你之前已经构造好、带有效DataFrame的
batch_request对象即可:
checkpoint_result = context.run_checkpoint( checkpoint_name="my_checkpoint_2", batch_request=batch_request )
方案2:运行Checkpoint时覆盖传入runtime_parameters
- 修改Checkpoint的YAML配置,仅移除
runtime_parameters字段,保留其余BatchRequest相关配置:
name: my_checkpoint_2 config_version: 1 class_name: SimpleCheckpoint validations: - batch_request: datasource_name: example_datasource data_connector_name: default_runtime_data_connector_name data_asset_name: MyDataAsset batch_identifiers: default_identifier_name: default_identifier expectation_suite_name: My_validations
- 运行Checkpoint时动态传入
runtime_parameters参数,把内存中的df传入:
checkpoint_result = context.run_checkpoint( checkpoint_name="my_checkpoint_2", runtime_parameters={"batch_data": df} )
两种方案任选其一即可解决报错。
内容的提问来源于stack exchange,提问作者Valentyn
相关产品推荐
相关产品推荐

