การทดสอบ schema
ทีมงานแก้ไขค่าใน DataFrame ที่ชื่อ expected แล้ว แต่ Polars อนุมานประเภทของ total_checkouts เป็น 64-bit ในขณะที่ pipeline ผลิตเป็น 32-bit โดย schema ทั้งสองถูกแสดงไว้ให้แล้วเพื่อให้เห็นความไม่ตรงกัน ให้แก้ไข dtype ของ expected จากนั้น assert ว่า schema ทั้งสองตรงกัน
actual, expected, และ assert_schema_equal ถูกโหลดไว้ล่วงหน้าแล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การปรับขนาดและเพิ่มประสิทธิภาพ Data Pipeline ด้วย Polars
คำแนะนำการฝึกหัด
- Cast
total_checkoutsในexpectedให้เป็นpl.Int32 - Assert ว่า schema ของ
actualและexpectedเท่ากัน
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Match the dtype the pipeline produces
expected = expected.with_columns(
pl.col("total_checkouts").cast(pl.____)
)
# Schema-level assertion
____(actual.schema, expected.schema)
print("Schema assertion passed.")
print(actual.schema)