跳到内容

Great Expectations

软件包版本

Great Expectations 集成已不再受支持。您可以使用以下软件包版本重新创建集成

- ydata-profiling==2.1.0 
- great-expectations==0.13.4

Great Expectations 是一个基于 Python 的开源库,用于验证、文档化和分析您的数据。它可以帮助您维护数据质量并改善团队之间关于数据的沟通。使用 Great Expectations,您可以断言您对加载和转换的数据的期望,并快速捕获数据问题——期望基本上是您数据的单元测试ydata-profiling 提供了一种方法,可以根据 ProfileReport 的结果创建一套期望!

关于 Great Expectations

期望是对您数据的断言。在 Great Expectations 中,这些断言以声明性语言表达,形式为简单、人类可读的 Python 方法。例如,为了断言您希望数据集中列 passenger_count 中的值是介于 1 到 6 之间的整数,您可以这样说

expect_column_values_to_be_between(column="passenger_count", min_value=1, max_value=6)

然后,Great Expectations 使用此语句来验证给定表中列 passenger_count 的值是否确实介于 1 到 6 之间,并返回成功或失败结果。该库目前提供了 数十种高度表达性的内置期望,并允许您编写自定义期望。

Great Expectations 将期望渲染为清晰、人类可读的文档,称为 Data Docs。这些 HTML 文档包含您的期望套件以及每次运行验证时的数据验证结果——将其视为持续更新的数据质量报告。

有关 Great Expectations 的更多信息,请查看 Great Expectations 文档 并加入 Great Expectations Slack 频道 获取帮助。

使用 ydata-profiling 创建期望套件

期望套件就是一组期望。您可以通过编写单独的语句(例如上面的语句)来创建期望套件,或者根据分析器结果自动生成它们。

ydata-profiling 提供了一个简单的 to_expectation_suite() 方法,该方法返回一个包含一组期望的 Great Expectations ExpectationSuite 对象。

前提条件:为了运行 to_expectation_suite() 方法,您需要使用 pip install great_expectations 安装 Great Expectations。

如果您想使用附加功能,例如保存套件和构建 Data Docs,您还需要在项目目录中运行 great_expectations init 来配置 Great Expectations Data Context。

获取您的期望集
import pandas as pd
from ydata_profiling import ProfileReport

df = pd.read_csv("titanic.csv")

profile = ProfileReport(df, title="YData Profiling Report", explorative=True)

# Obtain an Expectation Suite with a set of default Expectations
# By default, this also profiles the dataset, saves the suite, runs validation, and builds Data Docs
suite = profile.to_expectation_suite()

这假设 great_expectations Data Context 目录与您运行脚本的路径相同。为了指定 Data Context 的位置,将其作为参数传入。

生成期望套件
1
2
3
4
5
6
import great_expectations as ge

data_context = ge.data_context.DataContext(
    context_root_dir="/Users/panda/code/my_ge_project/"
)
suite = profile.to_expectation_suite(data_context=data_context)

您也可以在函数调用中单独配置每个特性

配置特性
1
2
3
4
5
6
7
8
suite = profile.to_expectation_suite(
    suite_name="titanic_expectations",
    data_context=data_context,
    save_suite=False,
    run_validation=False,
    build_data_docs=False,
    handler=handler,
)

请参阅Great Expectations 示例获取完整的示例。