总结

使用 Great Expectations 的数据质量入门

Davina Moossazadeh

Data Scientist

第1章:连接数据

创建数据上下文

context = gx.get_context()
使用 Great Expectations 的数据质量入门

第1章:连接数据

连接数据

从数据上下文创建数据源:

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

从数据源创建数据资产:

data_asset = data_source.add_dataframe_asset(
    name: str
)
使用 Great Expectations 的数据质量入门

第1章:连接数据

批量读取数据

从数据资产创建批次定义:

batch_definition = data_asset. \
add_batch_definition_whole_dataframe(
  name: str
)

从批次定义创建批次:

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

$$

获取批次 DataFrame 的行:

batch.head(fetch_all: bool)  

获取批次 DataFrame 的列名列表:

batch.columns()
使用 Great Expectations 的数据质量入门

第2章:制定期望

创建期望

创建一个期望: gx.expectations.Expect...(...)

创建行数期望:

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

$$

验证期望:

validation_results = batch.validate(
    expect=expectation
)

查看验证结果:

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

第2章:制定期望

架构类期望

形状类期望:

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

$$

列名类期望:

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

第2章:制定期望

创建期望集

创建期望集:

suite = gx.ExpectationSuite(name: str)

向期望集添加期望:

suite.add_expectation(expectation)

访问期望集内容:

suite.expectations

$$

验证期望集:

validation_results = batch.validate(
    expect=suite
)

查看验证结果:

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

第2章:制定期望

验证期望集

将期望集添加到数据上下文:

context.suites.add(suite)

创建验证定义:

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

$$

运行验证:

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

查看验证结果:

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

第3章:GX 实战

部署验证定义

将验证定义添加到数据上下文:

context.validation_definitions.add(
    validation_definition
)

创建检查点:

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

$$

运行检查点:

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

查看检查点结果:

checkpoint_results.success
使用 Great Expectations 的数据质量入门

第3章:GX 实战

更新期望集

复制期望:

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

检查期望是否在期望集中:

expectation in suite.expectations

删除期望:

suite.delete_expectation(expectation)

$$

更新期望的值:

expectation.value = new_value

保存期望更改:

expectation.save()

保存期望集更改:

suite.save()
使用 Great Expectations 的数据质量入门

第3章:GX 实战

管理组件

向数据上下文添加组件:

context.data_sources.add(data_source)

context.suites.add(suite)

context.validation_definitions.add(
    validation_definition
)

context.checkpoints.add(checkpoint)

$$

检索组件:

.get(name: str)

列出组件:

.all()

删除组件:

.delete(name: str)
使用 Great Expectations 的数据质量入门

第4章:期望全览

基础列期望

行级期望:

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

$$

聚合级期望:

ExpectColumnDistinctValuesToEqualSet(
    column: str, value_set: set
)
ExpectColumnUniqueValueCountToBeBetween(
    column: str, 
    min_value: int, max_value: int
)
ExpectColumnValuesToBeUnique(column: str)
ExpectColumnMostCommonValueToBeInSet(
    column: str, value_set: set
)
使用 Great Expectations 的数据质量入门

第4章:期望全览

类型特定期望

数值类期望:

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

$$

字符串类期望:

ExpectColumnValueLengthsToEqual(
  column: str, value: int
)
ExpectColumnValuesToMatchRegex(
  column: str, regex: str
)
ExpectColumnValuesToMatchRegexList(
  column: str, regex_list: list
)
ExpectColumnValuesToBe{Dateutil,Json}Parseable(
  column: str
)
使用 Great Expectations 的数据质量入门

第4章:期望全览

条件期望

expectation = gx.expectations.Expect...(
    expetation_parameters,
    ...,
    condition_parser='pandas',
    row_condition: str,
)
使用 Great Expectations 的数据质量入门

结束语

使用 Great Expectations 的数据质量入门

恭喜!

使用 Great Expectations 的数据质量入门

Preparing Video For Download...