更新 Expectation Suite

Great Expectations 資料品質入門

Davina Moossazadeh

Data Scientist

加入 Expectations

expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=10
)
suite = gx.ExpectationSuite(
    name="my_suite"
)
# Add Expectation to Suite
suite.add_expectation(
    expectation=expectation
)
Great Expectations 資料品質入門

多個 Expectation Suite

# Create another Expectation Suite
another_suite = gx.ExpectationSuite(name="my_other_suite")
# Add the same Expectation to the new Suite
another_suite.add_expectation(expectation=expectation)

Expectations 不能同時隸屬多個 Suite:

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`.
Great Expectations 資料品質入門

複製 Expectations

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`.

複製該 Expectation,將其 .id 設為 None,即可無誤加入新 Suite:

expectation_copy = expectation.copy()

expectation_copy.id = None
another_suite.add_expectation(
    expectation=expectation_copy
)
Great Expectations 資料品質入門

檢查 Expectations

print(
    expectation_copy in another_suite.expectations
)
True
Great Expectations 資料品質入門

刪除 Expectations

新增

.add_expectation()

suite.add_expectation(
    expectation=expectation
)

刪除

.delete_expectation()

suite.delete_expectation(
    expectation=expectation
)
Great Expectations 資料品質入門

調整 Expectations

更新 .value 屬性並儲存變更:

expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=10
)

expectation.value = 11
expectation.save()

確保該 Expectation 已隸屬某個 Suite,否則會出現:

RuntimeError: Expectation must be added to ExpectationSuite before it can be saved.
Great Expectations 資料品質入門

更新一個 Expectation Suite

suite = gx.ExpectationSuite(name="my_suite") 
validation_definition = gx.ValidationDefinition(
    data=batch_definition, suite=suite, name="my_validation_definition"
)
# Define Expectation
col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")

# Add Expectation to Suite suite.add_expectation(expectation=col_name_expectation)
# Run Validation Definition associated with the Suite validation_results = validation_definition.run()
Great Expectations 資料品質入門

儲存 Expectation Suite

在執行 Validation Definition 前,先儲存 Suite 的變更以避免錯誤:

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.
Great Expectations 資料品質入門

儲存 Expectation Suite

使用 .save() 儲存 Suite,然後執行 Validation Definition 以避免錯誤:

suite.save()

validation_results = validation_definition.run()
print(validation_results.success)
False
Great Expectations 資料品質入門

小抄

複製 Expectation:

expectation_copy = expectation.copy()
expectation_copy.id = None

檢查 Expectation 是否在 Suite 中:

expectation in suite.expectations

刪除 Expectation:

suite.delete_expectation(expectation)

更新 Expectation 的值:

expectation.value = new_value

儲存 Expectation 的變更:

expectation.save()

儲存 Expectation Suite 的變更:

suite.save()
Great Expectations 資料品質入門

一起來練習吧!

Great Expectations 資料品質入門

Preparing Video For Download...