创建 Expectations

使用 Great Expectations 的数据质量入门

Davina Moossazadeh

Data Scientist

Expectations

Expectation:关于数据的可验证断言

  • 列级 Expectations
  • 形状与架构(schema)Expectations
    • schema:数据集结构的蓝图
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
使用 Great Expectations 的数据质量入门

Expectations

Expectation:关于数据的可验证断言

  • 列级 Expectations
  • 形状与架构(schema)Expectations
    • schema:数据集结构的蓝图
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
使用 Great Expectations 的数据质量入门

可再生能源发电数据集

一个包含可再生能源发电数据的 pandas DataFrame,列包括:"Time""Energy delta[Wh]""GHI""temp""pressure""humidity""wind_speed""rain_1h""snow_1h""clouds_all"。该 DataFrame 有 118,066 行。

1 https://www.kaggle.com/datasets/pythonafroz/renewable-power-generation-and-weather-conditions
使用 Great Expectations 的数据质量入门

创建一个 Expectation

gx.expectations.Expect...(...)

GX 类(Expectations):PascalCase

GX 函数/方法:snake_case

1 https://docs.greatexpectations.io/docs/core/define_expectations/create_an_expectation/
使用 Great Expectations 的数据质量入门

创建一个 Expectation

row_count_expectation = gx.expectations.ExpectTableRowCountToEqual(

value=118000 )
validation_results = batch.validate(
expect=row_count_expectation )
1 https://docs.greatexpectations.io/docs/core/define_expectations/create_an_expectation/ https://docs.greatexpectations.io/docs/core/define_expectations/test_an_expectation/
使用 Great Expectations 的数据质量入门

评估一个 Expectation

print(validation_results)
{ 
    "success": false,
    "expectation_config": {
        "type": "expect_table_row_count_to_equal",
        "kwargs": {"batch_id": "my_pandas_datasource-my_dataframe_asset", "value": 118000},
        "meta": {},
        "rendered_content": [{"name": "atomic.prescriptive.summary", "value": {"schema": {"type": "com.superconductive.rendered.string"}, "template": "Must have exactly $value rows.", "params": {"value": {"schema": {"type": "number"}, "value": 118000}}}, "value_type": "StringValueType"}]
    },
    "result": {"observed_value": 118066},
    "meta": {},
    "exception_info": {"raised_exception": false, "exception_traceback": null, "exception_message": null},
    "rendered_content": [{"name": "atomic.diagnostic.observed_value", "value": {"schema": {"type": "com.superconductive.rendered.string"}, "template": "118066", "params": {}}, "value_type": "StringValueType"}]
}
1 https://docs.greatexpectations.io/docs/core/run_validations/run_a_validation_definition/
使用 Great Expectations 的数据质量入门

评估一个 Expectation

print(validation_results.describe())
{
    "expectation_type": "expect_table_row_count_to_equal",
    "success": false,
    "kwargs": {
        "batch_id": "my_pandas_datasource-my_dataframe_asset",
        "value": 118000
    },
    "result": {
        "observed_value": 118066
    }
}
使用 Great Expectations 的数据质量入门

评估一个 Expectation

print(validation_results.success)
False
print(validation_results["success"])
False
使用 Great Expectations 的数据质量入门

评估一个 Expectation

print(validation_results.result)
{'observed_value': 118066}
print(validation_results["result"])
{'observed_value': 118066}
使用 Great Expectations 的数据质量入门

其他常见 Expectations

形状类 Expectations:

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

列名类 Expectations:

ExpectTableColumnsToMatchSet(
    column_set: set
)
ExpectColumnToExist(column: str)
使用 Great Expectations 的数据质量入门

速查表

创建一个 Expectation:

gx.expectations.Expect...(...)

验证一个 Expectation:

validation_results = batch.validate(
    expect=expectation
)

创建行数 Expectation:

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

查看验证结果:

validation_results.describe()
validation_results.success
validation_results.result
使用 Great Expectations 的数据质量入门

Passons à la pratique !

使用 Great Expectations 的数据质量入门

Preparing Video For Download...