Memperbarui Expectation Suite

Pengantar Data Quality dengan Great Expectations

Davina Moossazadeh

Data Scientist

Menambahkan Expectation

expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=10
)
suite = gx.ExpectationSuite(
    name="my_suite"
)
# Tambahkan Expectation ke Suite
suite.add_expectation(
    expectation=expectation
)
Pengantar Data Quality dengan Great Expectations

Beberapa Expectation Suite

# 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`.
Pengantar Data Quality dengan Great Expectations

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

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
)
Pengantar Data Quality dengan Great Expectations

Memeriksa Expectation

print(
    expectation_copy in another_suite.expectations
)
True
Pengantar Data Quality dengan Great Expectations

Menghapus Expectation

Tambah

.add_expectation()

suite.add_expectation(
    expectation=expectation
)

Hapus

.delete_expectation()

suite.delete_expectation(
    expectation=expectation
)
Pengantar Data Quality dengan Great Expectations

Menyesuaikan Expectation

Perbarui atribut .value dan simpan perubahan:

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

expectation.value = 11
expectation.save()

Pastikan Expectation berada dalam Suite, jika tidak:

RuntimeError: Expectation must be added to ExpectationSuite before it can be saved.
Pengantar Data Quality dengan Great Expectations

Memperbarui Expectation Suite

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()
Pengantar Data Quality dengan Great Expectations

Menyimpan Expectation Suite

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.
Pengantar Data Quality dengan Great Expectations

Menyimpan Expectation Suite

Gunakan metode .save() untuk menyimpan Suite dan menjalankan Validation Definition tanpa error:

suite.save()

validation_results = validation_definition.run()
print(validation_results.success)
False
Pengantar Data Quality dengan Great Expectations

Cheat sheet

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

Ayo berlatih!

Pengantar Data Quality dengan Great Expectations

Preparing Video For Download...