Wrap-Up

Introduction to Data Quality with Great Expectations

Davina Moossazadeh

Data Scientist

Chapter 1: Connecting to Data

Create a Data Context

context = gx.get_context()
Introduction to Data Quality with Great Expectations

Chapter 1: Connecting to Data

Connect to Data

Create Data Source from Data Context:

data_source = context.data_sources.add_pandas(
    name: str
)

Create Data Asset from Data Source:

data_asset = data_source.add_dataframe_asset(
    name: str
)
Introduction to Data Quality with Great Expectations

Chapter 1: Connecting to Data

Read Data in Batches

Create Batch Definition from Data Asset:

batch_definition = data_asset. \
add_batch_definition_whole_dataframe(
  name: str
)

Create Batch from Batch Definition:

batch = batch_definition.get_batch(
  batch_parameters={"dataframe": dataframe}
)

$$

Get Batch DataFrame rows:

batch.head(fetch_all: bool)  

Get Batch DataFrame column list:

batch.columns()
Introduction to Data Quality with Great Expectations

Chapter 2: Establishing Expectations

Create Expectations

Create an Expectation: gx.expectations.Expect...(...)

Create a row count Expectation:

expectation = gx.expectations. \
ExpectTableRowCountToEqual(
    value: int
)

$$

Validate Expectation:

validation_results = batch.validate(
    expect=expectation
)

Check Validation Results:

validation_results.describe()
validation_results.success
validation_results.result
Introduction to Data Quality with Great Expectations

Chapter 2: Establishing Expectations

Schema Expectations

Shape Expectations:

ExpectTableRowCountToEqual(value: int)
ExpectTableRowCountToBeBetween(
    min_value: int, max_value: int
)
ExpectTableColumnCountToEqual(
    value: int
)
ExpectTableColumnCountToBeBetween(
    min_value: int, max_value: int
)

$$

Column name Expectations:

ExpectTableColumnsToMatchSet(
    column_set: set
)
ExpectColumnToExist(column: str)
Introduction to Data Quality with Great Expectations

Chapter 2: Establishing Expectations

Create a Suite of Expectations

Create Expectation Suite:

suite = gx.ExpectationSuite(name: str)

Add Expectation to Suite:

suite.add_expectation(expectation)

Access Suite's Expectations:

suite.expectations

$$

Validate Expectation Suite:

validation_results = batch.validate(
    expect=suite
)

Check Validation Results:

validation_results.success
validation_results.describe()
Introduction to Data Quality with Great Expectations

Chapter 2: Establishing Expectations

Validate Expectation Suites

Add Expectation Suite to Data Context:

context.suites.add(suite)

Create Validation Definition:

validation_definition = \
gx.ValidationDefinition(
  name: str, 
  data=batch_definition, 
  suite=suite
)

$$

Run Validation:

validation_results = \
validation_definition.run(
  batch_parameters={"dataframe": dataframe}
)

Check Validation Results:

validation_results.success
validation_results.describe()
Introduction to Data Quality with Great Expectations

Chapter 3: GX in Practice

Deploy Validation Definitions

Add Validation Definition to Data Context:

context.validation_definitions.add(
    validation_definition
)

Create Checkpoint:

checkpoint = gx.Checkpoint(
    name: str, 
    validation_definitions: list,
)

$$

Run Checkpoint:

checkpoint_results = checkpoint.run(
    batch_parameters={
        "dataframe": dataframe
    }
)

Check Checkpoint Results:

checkpoint_results.success
Introduction to Data Quality with Great Expectations

Chapter 3: GX in Practice

Update Expectation Suites

Copy Expectation:

expectation_copy = expectation.copy()
expectation_copy.id = None

Check if Expectation is in Suite:

expectation in suite.expectations

Delete Expectation:

suite.delete_expectation(expectation)

$$

Update Expectation value:

expectation.value = new_value

Save changes to Expectation:

expectation.save()

Save changes to Expectation Suite:

suite.save()
Introduction to Data Quality with Great Expectations

Chapter 3: GX in Practice

Manage Components

Add a component to Data Context:

context.data_sources.add(data_source)

context.suites.add(suite)

context.validation_definitions.add(
    validation_definition
)

context.checkpoints.add(checkpoint)

$$

Retrieve a component:

.get(name: str)

List components:

.all()

Delete a component:

.delete(name: str)
Introduction to Data Quality with Great Expectations

Chapter 4: All About Expectations

Basic Column Expectations

Row-level Expectations:

ExpectColumnValuesToNotBeNull(
    column: str
)
ExpectColumnValuesToBeOfType(
    column: str, type_: str
)

$$

Aggregate-level Expectations:

ExpectColumnDistinctValuesToEqualSet(
    column: str, value_set: set
)
ExpectColumnUniqueValueCountToBeBetween(
    column: str, 
    min_value: int, max_value: int
)
ExpectColumnValuesToBeUnique(column: str)
ExpectColumnMostCommonValueToBeInSet(
    column: str, value_set: set
)
Introduction to Data Quality with Great Expectations

Chapter 4: All About Expectations

Type-Specific Expectations

Numeric Expectations:

ExpectColumn<METRIC>ToBeBetween(
  column: str, min_value: int, max_value: int
)
# <METRIC> in 
# {"Mean", "Median", "Stdev", "Sum"}
ExpectColumnValuesToBeBetween(
  column: str, min_value: int, max_value: int
)
ExpectColumnValuesToBeIncreasing(column: str)
ExpectColumnValuesToBeDecreasing(column: str)

$$

String Expectations:

ExpectColumnValueLengthsToEqual(
  column: str, value: int
)
ExpectColumnValuesToMatchRegex(
  column: str, regex: str
)
ExpectColumnValuesToMatchRegexList(
  column: str, regex_list: list
)
ExpectColumnValuesToBe{Dateutil,Json}Parseable(
  column: str
)
Introduction to Data Quality with Great Expectations

Chapter 4: All About Expectations

Conditional Expectations

expectation = gx.expectations.Expect...(
    expetation_parameters,
    ...,
    condition_parser='pandas',
    row_condition: str,
)
Introduction to Data Quality with Great Expectations

Final Words

Introduction to Data Quality with Great Expectations

Congratulations!

Introduction to Data Quality with Great Expectations

Preparing Video For Download...