使用 Great Expectations 的数据质量入门
Davina Moossazadeh
Data Scientist
expectation = gx.expectations.ExpectTableColumnCountToEqual(
value=10
)
suite = gx.ExpectationSuite(
name="my_suite"
)
# 将期望添加到套件
suite.add_expectation(
expectation=expectation
)
# 创建另一个期望套件
another_suite = gx.ExpectationSuite(name="my_other_suite")
# 将同一期望添加到新套件
another_suite.add_expectation(expectation=expectation)
期望不能同时属于多个套件:
RuntimeError: Cannot add Expectation because it already belongs to an
ExpectationSuite. If you want to update an existing Expectation, please call
Expectation.save(). If you are copying this Expectation to a new ExpectationSuite,
please copy it first (the core expectations and some others support
copy(expectation)) and set `Expectation.id = None`.
If you are copying this Expectation to a new ExpectationSuite, please copy it first
(the core expectations and some others support copy(expectation)) and set
`Expectation.id = None`.
将期望复制,设置其 .id 为 None,并无错误地添加到新套件:
expectation_copy = expectation.copy()expectation_copy.id = None
another_suite.add_expectation(
expectation=expectation_copy
)
print(
expectation_copy in another_suite.expectations
)
True
添加
.add_expectation()
suite.add_expectation(
expectation=expectation
)
删除
.delete_expectation()
suite.delete_expectation(
expectation=expectation
)
更新 .value 属性并保存更改:
expectation = gx.expectations.ExpectTableColumnCountToEqual( value=10 )expectation.value = 11expectation.save()
确保该期望隶属于某个套件,否则:
RuntimeError: Expectation must be added to ExpectationSuite before it can be saved.
suite = gx.ExpectationSuite(name="my_suite")
validation_definition = gx.ValidationDefinition(
data=batch_definition, suite=suite, name="my_validation_definition"
)
# 定义期望 col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")# 将期望添加到套件 suite.add_expectation(expectation=col_name_expectation)# 运行与套件关联的验证定义 validation_results = validation_definition.run()
在运行验证定义前保存套件更改,以避免错误:
validation_results = validation_definition.run()
ResourceFreshnessAggregateError: ExpectationSuite 'my_suite' has changed since it
has last been saved. Please update with `<SUITE_OBJECT>.save()`, then try your
action again.
使用 .save() 保存套件,然后无错误地运行验证定义:
suite.save()validation_results = validation_definition.run()print(validation_results.success)
False
复制期望:
expectation_copy = expectation.copy()
expectation_copy.id = None
检查期望是否在套件中:
expectation in suite.expectations
删除期望:
suite.delete_expectation(expectation)
更新期望的值:
expectation.value = new_value
保存期望的更改:
expectation.save()
保存期望套件的更改:
suite.save()
使用 Great Expectations 的数据质量入门