Conditional Expectations

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

Davina Moossazadeh

Data Scientist

Conditional Expectations คืออะไร?

Conditional Expectations - Expectations สำหรับชุดข้อมูลย่อย

เพราะบางตัวแปรขึ้นอยู่กับค่าของตัวแปรอื่น

ตัวอย่างเช่น:

  • Expectation ที่กำหนดให้ค่าในคอลัมน์ star_rating ต้องเป็น 0 สำหรับทุกแถวที่มีค่า 0 ใน review_count
1 https://docs.greatexpectations.io/docs/core/customize_expectations/expectation_conditions/
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

Syntax ของ Conditional Expectations

Dataset Expectations แปลงเป็น Conditional Expectations ได้โดยเพิ่มอาร์กิวเมนต์ 2 ตัว:

  1. row_condition
    • นิพจน์บูลีนในรูปแบบ string สำหรับกำหนดชุดข้อมูลย่อยที่ Conditional Expectation จะใช้
  2. condition_parser
    • string ที่กำหนด syntax ของ row_condition
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

condition parser

เมื่อใช้ Conditional Expectations กับ pandas ต้องกำหนดอาร์กิวเมนต์นี้เป็น "pandas"

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

row condition

pandas Syntax
df["foo"] == 'Two Two'
df["foo"].notNull()
df["foo"] <= datetime.date(2023, 3, 13)
(df["foo"] < 5) & (df["foo"] >= 3.14)

df["foo"].str.startswith("bar")
Great Expectations row_condition
'foo == "Two Two"'
'foo.notNull()'
'foo <= datetime.date(2023, 3, 13)'
'(foo > 5) & (foo <= 3.14)'
'foo > 5 and foo <= 3.14'
'foo.str.startswith("bar")'
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

row condition

กฎการใช้งาน
  1. ห้ามใช้ single quote ภายใน

    • row_condition="foo=='Two Two'"
    • row_condition='foo=="Two Two"' เครื่องหมายถูกแสดงว่า syntax ของ `row_condition` ในบุลเล็ตที่สองถูกต้อง
  2. ห้ามใช้การขึ้นบรรทัดใหม่ภายใน

    • row_condition="""
      foo=="Two Two"
      """  
      
    • row_condition='foo=="Two Two"' เครื่องหมายถูกแสดงว่า syntax ของ `row_condition` ในบุลเล็ตที่สองถูกต้อง
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

ตัวอย่าง Expectation: star rating

ไม่มีเงื่อนไข
expectation = gx.expectations.\
ExpectColumnValuesToBeBetween(
    column="price_usd",
    max_value=10,
)
validation_results = batch.validate(
    expect=expectation
)
print(validation_results.success)
False
มีเงื่อนไข
expectation = gx.expectations.\
ExpectColumnValuesToBeBetween(
    column="price_usd",
    max_value=10,
    condition_parser='pandas',
    row_condition='mark_price_usd < 10',
)
validation_results = batch.validate(
    expect=expectation
)
print(validation_results.success)
True
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

มาฝึกกันเถอะ!

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

Preparing Video For Download...