สรุปเนื้อหา

การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

Davina Moossazadeh

Data Scientist

บทที่ 1: การเชื่อมต่อข้อมูล

สร้าง Data Context

context = gx.get_context()
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 1: การเชื่อมต่อข้อมูล

เชื่อมต่อข้อมูล

สร้าง Data Source จาก Data Context:

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

สร้าง Data Asset จาก Data Source:

data_asset = data_source.add_dataframe_asset(
    name: str
)
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 1: การเชื่อมต่อข้อมูล

อ่านข้อมูลแบบ Batch

สร้าง Batch Definition จาก Data Asset:

batch_definition = data_asset. \
add_batch_definition_whole_dataframe(
  name: str
)

สร้าง Batch จาก Batch Definition:

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

$$

ดึงแถวของ Batch DataFrame:

batch.head(fetch_all: bool)  

ดึงรายการคอลัมน์ของ Batch DataFrame:

batch.columns()
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 2: การกำหนด Expectation

สร้าง Expectation

สร้าง Expectation: gx.expectations.Expect...(...)

สร้าง Expectation สำหรับจำนวนแถว:

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

$$

ตรวจสอบ Expectation:

validation_results = batch.validate(
    expect=expectation
)

ดูผลการตรวจสอบ:

validation_results.describe()
validation_results.success
validation_results.result
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 2: การกำหนด Expectation

Schema Expectations

Expectation เกี่ยวกับรูปร่างข้อมูล:

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

$$

Expectation เกี่ยวกับชื่อคอลัมน์:

ExpectTableColumnsToMatchSet(
    column_set: set
)
ExpectColumnToExist(column: str)
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 2: การกำหนด Expectation

สร้าง Expectation Suite

สร้าง Expectation Suite:

suite = gx.ExpectationSuite(name: str)

เพิ่ม Expectation เข้า Suite:

suite.add_expectation(expectation)

เข้าถึง Expectation ใน Suite:

suite.expectations

$$

ตรวจสอบ Expectation Suite:

validation_results = batch.validate(
    expect=suite
)

ดูผลการตรวจสอบ:

validation_results.success
validation_results.describe()
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 2: การกำหนด Expectation

ตรวจสอบ Expectation Suite

เพิ่ม Expectation Suite เข้า Data Context:

context.suites.add(suite)

สร้าง Validation Definition:

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

$$

รัน Validation:

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

ดูผลการตรวจสอบ:

validation_results.success
validation_results.describe()
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 3: GX ในทางปฏิบัติ

ติดตั้งใช้งาน Validation Definition

เพิ่ม Validation Definition เข้า Data Context:

context.validation_definitions.add(
    validation_definition
)

สร้าง Checkpoint:

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

$$

รัน Checkpoint:

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

ดูผลลัพธ์ของ Checkpoint:

checkpoint_results.success
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 3: GX ในทางปฏิบัติ

อัปเดต Expectation Suite

คัดลอก Expectation:

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

ตรวจสอบว่า Expectation อยู่ใน Suite หรือไม่:

expectation in suite.expectations

ลบ Expectation:

suite.delete_expectation(expectation)

$$

อัปเดตค่าของ Expectation:

expectation.value = new_value

บันทึกการเปลี่ยนแปลง Expectation:

expectation.save()

บันทึกการเปลี่ยนแปลง Expectation Suite:

suite.save()
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 3: GX ในทางปฏิบัติ

จัดการ Component

เพิ่ม Component เข้า Data Context:

context.data_sources.add(data_source)

context.suites.add(suite)

context.validation_definitions.add(
    validation_definition
)

context.checkpoints.add(checkpoint)

$$

ดึง Component:

.get(name: str)

แสดงรายการ Component:

.all()

ลบ Component:

.delete(name: str)
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

บทที่ 4: Expectation ทุกรูปแบบ

Expectation พื้นฐานระดับคอลัมน์

Expectation ระดับแถว:

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

$$

Expectation ระดับ Aggregate:

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: Expectation ทุกรูปแบบ

Expectation เฉพาะประเภทข้อมูล

Expectation สำหรับตัวเลข:

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)

$$

Expectation สำหรับ String:

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 ทุกรูปแบบ

Expectation แบบมีเงื่อนไข

expectation = gx.expectations.Expect...(
    expetation_parameters,
    ...,
    condition_parser='pandas',
    row_condition: str,
)
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

คำกล่าวปิด

การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

ยินดีด้วย!

การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

Preparing Video For Download...