Skip to main content
Version: 1.0 prerelease

Manage Expectation Suites

An Expectation Suite contains a group of Expectations that describe the same set of data. All the Expectations that you apply to your data are grouped into an Expectation Suite.

Prerequisites

Create an Expectation Suite

  1. Import the GX Core library and the ExpectationSuite class:
Python code
import great_expectations as gx
from great_expectations.core.expectation_suite import ExpectationSuite
  1. Request a Data Context.

In this example the variable context is your Data Context.

  1. Create an Expectation Suite:
Python code
new_suite_name = "my_first_expectation_suite"
suite = ExpectationSuite(name=new_suite_name)
  1. Add the Expectation Suite to your Data Context:
Python code
suite = context.suites.add(suite)
tip

You can add an Expectation Suite to your Data Context at the same time as you create the Expectation Suite with the following code:

Python code
new_suite_name = "my_second_expectation_suite"
suite = context.suites.add(ExpectationSuite(name=new_suite_name))

Get an existing Expectation Suite

  1. Request a Data Context.

In this example the variable context is your Data Context.

  1. Use the Data Context to retrieve the existing Expectation Suite:
Python code
existing_suite_name = (
"my_expectation_suite" # replace this with the name of your Expectation Suite
)
suite = context.suites.get(name=existing_suite_name)

Rename an Expectation Suite

  1. Get the Expectation Suite to rename. This could be an existing Expectation Suite you retrieve from your Data Context or a new Expectation Suite that you have referenced earlier in your code.

In this example the variable suite is your Expectation Suite.

  1. Change the name attribute of the Expectation Suite:
Python code
suite.name = "my_renamed_expectation_suite"
  1. Save the changes to the Expectation Suite:
Python code
suite.save()

The suite.save() method will save all changes to the Expectation Suite, including changes that you have made to any Expectations within the Expectation Suite. If you have unsaved changes to Expectations in the Expectation Suite that you do not wish to keep, you should rename the Expectation Suite in a new Python session.

Delete an Expectation Suite

  1. Request a Data Context.

In this example the variable context is your Data Context.

  1. Get the Expectation Suite to delete:
Python code
suite_name = "my_deletable_expectation_suite"
suite = context.suites.get(suite_name)
  1. Use the Data Context to delete the retrieved Expectation Suite:
Python code
context.suites.delete(suite=suite)

Add Expectations to an Expectation Suite

  1. Create a new or get an existing Expectation Suite.

In this example the variable suite is your Expectation Suite.

  1. Create an Expectation.

In this example the variable expectation is the Expectation to add to the Expectation Suite.

  1. Add the Expectation to the Expectation Suite:
Python code
suite.add_expectation(expectation)
tip

You can create an Expectation at the same time as you add it to the Expectation Suite:

Python code
suite.add_expectation(gxe.ExpectColumnValuesToNotBeNull(column="pickup_datetime"))

Get an Expectation from an Expectation Suite

  1. Get an existing Expectation Suite that contains Expectations or add some Expectations to a new Expectation Suite.

In this example the variable suite is your Expectation Suite.

  1. Find the desired Expectation by iterating through the Expectations in the Expectation Suite and comparing classes and attributes to those of the desired Expectation:
Python code
expectation = next(
expectation
for expectation in suite.expectations
if isinstance(expectation, gxe.ExpectColumnValuesToNotBeNull)
and expectation.column == "pickup_datetime"
)

Edit a single Expectation in an Expectation Suite

  1. Get the Expectation to edit from its Expectation Suite.

In this example the variable expectation is the Expectation you want to edit.

  1. Modify the Expectation:
Python code
expectation.column = "pickup_location_id"
  1. Save the modified Expectation in the Expectation Suite:
Python code
expectation.save()

expectation.save() is explicitly used to update the configuration of an Expectation in an Expectation Suite.

An Expectation Suite continues to use the Expectation's original values unless you save your modifications. You can test changes to an Expectation without running expectation.save(), but those changes will not persist in the Expectation Suite until expectation.save() is run.

Edit multiple Expectations in an Expectation Suite

  1. Get an existing Expectation Suite that contains Expectations, or add some Expectations to a new Expectation Suite.

In this example the variable suite is your Expectation Suite.

  1. Modify multiple Expectations in the Expectation Suite:
Python code
for expectation in suite.expectations:
expectation.notes = "This Expectation was generated as part of GX Documentation."
  1. Save the Expectation Suite and all modifications to the Expectations within it:
Python code
suite.save()

Delete an Expectation from an Expectation Suite

  1. Get the Expectation Suite containing the Expectation to delete.

In this example the variable suite is the Expectation Suite containing the Expectation to delete.

  1. Get the Expectation to delete from its Expectation Suite.

In this example the variable expectation is the Expectation to delete.

  1. Use the Expectation Suite to delete the Expectation:
Python code
suite.delete_expectation(expectation=expectation_to_delete)