Schema Expectations

Introduction to Data Quality with Great Expectations

Davina Moossazadeh

Data Scientist

Shape and schema Expectations

Expectation - A verifiable assertion about data

  • Column Expectations
  • Shape and schema Expectations
    • schema - the blueprint of a dataset's structure
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
Introduction to Data Quality with Great Expectations

Row count

row_count_expectation = gx.expectations.ExpectTableRowCountToEqual(
    value=118000
)

validation_results = batch.validate(expect=row_count_expectation)
print(validation_results.success)
False
print(validation_results.result)
{'observed_value': 118066}
Introduction to Data Quality with Great Expectations

Row count range

Use ExpectTableRowCountToBeBetween to define a range for row counts:

row_count_expectation = gx.expectations.ExpectTableRowCountToBeBetween(

min_value=117000, max_value=119000 )
validation_results = batch.validate(expect=row_count_expectation)
print(validation_results.success)
True
print(validation_results.result)
{'observed_value': 118066}
Introduction to Data Quality with Great Expectations

Column count

col_count_expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=15
)
validation_results = batch.validate(expect=col_count_expectation)
print(validation_results.success)
False
print(validation_result.result)
{'observed_value': 18}
Introduction to Data Quality with Great Expectations

Column count range

Use ExpectTableColumnCountToBeBetween to define a range for column counts:

col_count_expectation = gx.expectations.ExpectTableColumnCountToBeBetween(
    min_value=14, max_value=18
)
validation_results = batch.validate(expect=col_count_expectation)
print(validation_results.success)
True
print(validation_result.result)
{'observed_value': 18}
Introduction to Data Quality with Great Expectations

Column name sets

Use ExpectTableColumnsToMatchSet to validate column names against a set:

expected_cols = ['clouds_all', 'snow_1h', 'rain_1h', 'wind_speed', 'humidity', 
'pressure', 'temp', 'GHI', 'Energy delta[Wh]', 'Time', 'Time']

col_names_expectation = gx.expectations.ExpectTableColumnsToMatchSet( column_set=expected_cols )
print(col_names_expectation.success, col_names_expectation.result)
True 
{'observed_value': ['Time', 'Energy delta[Wh]', 'GHI', 'temp', 'pressure',
'humidity', 'wind_speed', 'rain_1h', 'snow_1h', 'clouds_all'}
Introduction to Data Quality with Great Expectations

Individual column names

Use ExpectColumnToExist to check if a specific column is present in the dataset:

col_name_expectation = gx.expectations.ExpectColumnToExist(column="not_a_column")
validation_result = batch.validate(expect=col_name_expectation)
print(validation_result.success)
False
col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")
validation_result = batch.validate(expect=col_name_expectation)
print(validation_result.success)
True
Introduction to Data Quality with Great Expectations

Cheat sheet

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

Let's practice!

Introduction to Data Quality with Great Expectations

Preparing Video For Download...