Great Expectations部分断言失效:指定断言方法报错
问题描述
使用Great Expectations(版本0.18.5)对数据执行断言时,部分断言失效,报错信息如下:
'PandasDataset' object has no attribute 'expect_column_values_to_not_be_null_and_column_to_not_be_empty'
测试代码:
import great_expectations as ge import pandas as pd df_asset = ge.from_pandas(pd.DataFrame({'A': [1.1, 2.2, 3.3], 'B': [4.4, 5.5, 6.6]})) print(df_asset) df_asset.expect_column_values_to_not_be_null_and_column_to_not_be_empty("A")
其他断言(如df_asset.expect_column_values_to_not_be_null("A"))可正常运行,返回结果:
{ "success": true, "expectation_config": { "expectation_type": "expect_column_values_to_not_be_null", "kwargs": { "column": "A", "result_format": "BASIC" }, "meta": {} }, "result": { "element_count": 3, "unexpected_count": 0, "unexpected_percent": 0.0, "unexpected_percent_total": 0.0, "partial_unexpected_list": [] }, "meta": {}, "exception_info": { "raised_exception": false, "exception_traceback": null, "exception_message": null } }
问题原因与解决方案
原因
Great Expectations的PandasDataset类中不存在expect_column_values_to_not_be_null_and_column_to_not_be_empty这个内置断言方法,你使用的方法名属于误写或自定义,并非官方提供的标准断言。
解决方案
若需同时验证「列值无空值」和「列存在且包含数据」,可拆分执行官方提供的多个断言:
- 用
expect_column_to_exist确保目标列存在 - 用
expect_column_values_to_not_be_null验证列内所有值非空 - 用
expect_column_to_have_at_least_one_value验证列包含至少一条数据
示例代码:
# 验证列A存在 df_asset.expect_column_to_exist("A") # 验证列A所有值非空 df_asset.expect_column_values_to_not_be_null("A") # 验证列A至少包含一条数据 df_asset.expect_column_to_have_at_least_one_value("A")
如果需要合并断言结果进行统一判断,可自行封装逻辑:
def validate_column(col_name): check_exist = df_asset.expect_column_to_exist(col_name)["success"] check_no_null = df_asset.expect_column_values_to_not_be_null(col_name)["success"] check_has_data = df_asset.expect_column_to_have_at_least_one_value(col_name)["success"] return check_exist and check_no_null and check_has_data # 执行验证并打印结果 print(validate_column("A"))
内容的提问来源于stack exchange,提问作者Bruno123
相关产品推荐
相关产品推荐

