การทำ logging ภายในดาต้าไปป์ไลน์
ในแบบฝึกหัดนี้ เราจะย้อนกลับไปดูฟังก์ชันที่คุณเขียนไว้ในวิดีโอก่อนหน้า แล้วฝึกเพิ่ม logging เข้าไปในฟังก์ชันนั้น ซึ่งจะช่วยได้มากเวลาต้องแก้ไขข้อผิดพลาดหรือปรับเปลี่ยนลอจิก
pandas ถูกนำเข้ามาในชื่อ pd แล้ว นอกจากนี้ยังมีการนำเข้าโมดูล logging และตั้งค่าระดับ log เริ่มต้นเป็น "debug"
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
ETL and ELT ใน Python
คำแนะนำการฝึกหัด
- สร้าง log ระดับ info หลังจากการแปลงข้อมูล โดยส่งข้อความ:
"Transformed 'Order Date' column to type 'datetime'." - บันทึกค่า
.shapeของ DataFrame ที่ระดับ debug ทั้งก่อนและหลังการกรองข้อมูล
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
def transform(raw_data):
raw_data["Order Date"] = pd.to_datetime(raw_data["Order Date"], format="%m/%d/%y %H:%M")
clean_data = raw_data.loc[raw_data["Price Each"] < 10, :]
# Create an info log regarding transformation
logging.____("Transformed 'Order Date' column to type 'datetime'.")
# Create debug-level logs for the DataFrame before and after filtering
____(f"Shape of the DataFrame before filtering: {raw_data.shape}")
____(f"Shape of the DataFrame after filtering: {clean_data.shape}")
return clean_data
clean_sales_data = transform(raw_sales_data)