Great Expectationsで始めるデータ品質入門
Davina Moossazadeh
Data Scientist
expectation = gx.expectations.ExpectTableColumnCountToEqual(
value=10
)
suite = gx.ExpectationSuite(
name="my_suite"
)
# Suite に Expectation を追加
suite.add_expectation(
expectation=expectation
)
# 別の 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`.
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
)
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()
Expectation が Suite に属していることを確認してください。そうでない場合:
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"
)
# 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()
エラーを避けるため、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.
.save() メソッドで Suite を保存し、エラーなく Validation Definition を実行します:
suite.save()validation_results = validation_definition.run()print(validation_results.success)
False
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で始めるデータ品質入門