Expectation Suite の更新

Great Expectationsで始めるデータ品質入門

Davina Moossazadeh

Data Scientist

Expectation の追加

expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=10
)
suite = gx.ExpectationSuite(
    name="my_suite"
)
# Suite に Expectation を追加
suite.add_expectation(
    expectation=expectation
)
Great Expectationsで始めるデータ品質入門

複数の Expectation Suite

# 別の Expectation Suite を作成
another_suite = gx.ExpectationSuite(name="my_other_suite")
# 同じ Expectation を新しい Suite に追加
another_suite.add_expectation(expectation=expectation)

Expectation は同時に複数の 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で始めるデータ品質入門

Expectation のコピー

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 をコピーし、.idNone に設定して新しい Suite にエラーなく追加します:

expectation_copy = expectation.copy()

expectation_copy.id = None
another_suite.add_expectation(
    expectation=expectation_copy
)
Great Expectationsで始めるデータ品質入門

Expectation の確認

print(
    expectation_copy in another_suite.expectations
)
True
Great Expectationsで始めるデータ品質入門

Expectation の削除

追加

.add_expectation()

suite.add_expectation(
    expectation=expectation
)

削除

.delete_expectation()

suite.delete_expectation(
    expectation=expectation
)
Great Expectationsで始めるデータ品質入門

Expectation の調整

.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"
)
# Expectation を定義
col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")

# Suite に Expectation を追加 suite.add_expectation(expectation=col_name_expectation)
# Suite に紐づく Validation Definition を実行 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で始めるデータ品質入門

Let's practice!

Great Expectationsで始めるデータ品質入門

Preparing Video For Download...