Pengantar Data Quality dengan Great Expectations
Davina Moossazadeh
Data Scientist
expectation = gx.expectations.ExpectTableColumnCountToEqual(
value=10
)
suite = gx.ExpectationSuite(
name="my_suite"
)
# Tambahkan Expectation ke Suite
suite.add_expectation(
expectation=expectation
)
# Buat Expectation Suite lain
another_suite = gx.ExpectationSuite(name="my_other_suite")
# Tambahkan Expectation yang sama ke Suite baru
another_suite.add_expectation(expectation=expectation)
Expectation tidak bisa berada di beberapa Suite sekaligus:
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`.
Salin Expectation, atur .id ke None, lalu tambahkan ke Suite baru tanpa error:
expectation_copy = expectation.copy()expectation_copy.id = None
another_suite.add_expectation(
expectation=expectation_copy
)
print(
expectation_copy in another_suite.expectations
)
True
Tambah
.add_expectation()
suite.add_expectation(
expectation=expectation
)
Hapus
.delete_expectation()
suite.delete_expectation(
expectation=expectation
)
Perbarui atribut .value dan simpan perubahan:
expectation = gx.expectations.ExpectTableColumnCountToEqual( value=10 )expectation.value = 11expectation.save()
Pastikan Expectation berada dalam Suite, jika tidak:
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"
)
# Definisikan Expectation col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")# Tambahkan Expectation ke Suite suite.add_expectation(expectation=col_name_expectation)# Jalankan Validation Definition yang terkait dengan Suite validation_results = validation_definition.run()
Simpan perubahan pada Suite sebelum menjalankan Validation Definition untuk menghindari error:
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.
Gunakan metode .save() untuk menyimpan Suite dan menjalankan Validation Definition tanpa error:
suite.save()validation_results = validation_definition.run()print(validation_results.success)
False
Salin Expectation:
expectation_copy = expectation.copy()
expectation_copy.id = None
Periksa apakah Expectation ada di Suite:
expectation in suite.expectations
Hapus Expectation:
suite.delete_expectation(expectation)
Perbarui nilai Expectation:
expectation.value = new_value
Simpan perubahan pada Expectation:
expectation.save()
Simpan perubahan pada Expectation Suite:
suite.save()
Pengantar Data Quality dengan Great Expectations