การแปลงและจัดการวันที่

การแปลงข้อมูลด้วย Polars

Liam Brannigan

Data Scientist & Polars Contributor

ข้อมูล time series

กราฟ time series ของราคาตลาด

การแปลงข้อมูลด้วย Polars

ข้อมูล time series

กราฟ time series ของยอดขาย

การแปลงข้อมูลด้วย Polars

ข้อมูล time series

กราฟ time series ของยอดสตรีม

การแปลงข้อมูลด้วย Polars

ข้อมูล time series

กราฟ time series ของจำนวนนับ

การแปลงข้อมูลด้วย Polars

ชุดข้อมูลราคาไฟฟ้า

shape: (24, 3)
| time             | price | solar |
| ---              | ---   | ---   |
| str              | f64   | f64   |
|------------------|-------|-------|
| 05/07/2025 00:00 | 34.2  | 0.0   |
| ...              | ...   | ...   |
| 05/07/2025 12:00 | 0.0   | 462.0 |
  • time - ค่ารายชั่วโมง
  • price - ปอนด์ต่อเมกะวัตต์ชั่วโมง
  • solar - วัตต์ต่อตารางเมตร
  • solar > 400 = วันฤดูร้อนที่อากาศอบอุ่น
การแปลงข้อมูลด้วย Polars

ความสัมพันธ์ระหว่างราคาและพลังงานแสงอาทิตย์

กราฟ time series ของราคาไฟฟ้า

การแปลงข้อมูลด้วย Polars

ความสัมพันธ์ระหว่างราคาและพลังงานแสงอาทิตย์

กราฟ time series ของราคาไฟฟ้า

การแปลงข้อมูลด้วย Polars

ประเภทข้อมูล datetime ใน Polars

  • Date - วันที่ตามปฏิทิน - 2000-01-01
  • Datetime - วันที่และเวลา - 2000-01-01 12:00:00
  • Time - เวลาในวัน - 12:00:00
  • Duration - ช่วงเวลา - 3 hours
การแปลงข้อมูลด้วย Polars

การแปลง datetime อัตโนมัติ

pl.read_csv(
  "electricity_prices",

)
การแปลงข้อมูลด้วย Polars

การแปลง datetime อัตโนมัติ

pl.read_csv(
  "electricity_prices",
  try_parse_dates=True
)
  • รูปแบบ: YYYY-MM-DD
  • เช่น 2025-07-05
  • เช่น 2025-07-05 12:00:00
การแปลงข้อมูลด้วย Polars

ข้อมูลนี้ต้องแปลงด้วยตนเอง

| time             | price | solar |
| ---              | ---   | ---   |
| str              | f64   | f64   |
|------------------|-------|-------|
| 05/07/2025 00:00 | 34.2  | 0.0   |
| 05/07/2025 01:00 | 28.7  | 0.0   |
  • รูปแบบ: DD/MM/YYYY HH:MM
  • Polars ไม่สามารถตรวจจับรูปแบบนี้ได้อัตโนมัติ
การแปลงข้อมูลด้วย Polars

การแปลงด้วย strptime

prices.with_columns(
    pl.col("time").str.strptime(                             )
)
การแปลงข้อมูลด้วย Polars

การแปลงด้วย strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime,                 )
)
การแปลงข้อมูลด้วย Polars

การแปลงด้วย strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y      ")
)
การแปลงข้อมูลด้วย Polars

การแปลงด้วย strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y %H:%M")
)
การแปลงข้อมูลด้วย Polars

การแปลงด้วย strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y %H:%M")
)
| time                | price | solar |
| ---                 | ---   | ---   |
| datetime[us]        | f64   | f64   |
|---------------------|-------|-------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   |
| 2025-07-05 01:00:00 | 28.7  | 0.0   |
| 2025-07-05 02:00:00 | 27.5  | 0.0   |
การแปลงข้อมูลด้วย Polars

การดึงส่วนประกอบของ datetime

ตาราง time series

การแปลงข้อมูลด้วย Polars

การดึงส่วนประกอบของ datetime

ตาราง time series ที่ไฮไลต์ส่วนวันที่ในแถวหนึ่ง

การแปลงข้อมูลด้วย Polars

การดึงส่วนประกอบของ datetime

ตาราง time series ที่ไฮไลต์ส่วนเวลาในแถวหนึ่ง

การแปลงข้อมูลด้วย Polars

การดึงส่วนประกอบของ datetime

ตาราง time series ที่ไฮไลต์ส่วนชั่วโมงในแถวหนึ่ง

การแปลงข้อมูลด้วย Polars

การดึงวันที่

prices.with_columns(
    pl.col("time").dt.date()
)
การแปลงข้อมูลด้วย Polars

การดึงวันที่

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
การแปลงข้อมูลด้วย Polars

การดึงวันที่

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
| time                | price | solar | date       |
| ---                 | ---   | ---   | ---        |
| datetime[µs]        | f64   | f64   | date       |
|---------------------|-------|-------|------------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 2025-07-05 |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 2025-07-05 |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 2025-07-05 |
การแปลงข้อมูลด้วย Polars

การดึงเวลา

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
การแปลงข้อมูลด้วย Polars

การดึงเวลา

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
| time                | price | solar | time_of_day |
| ---                 | ---   | ---   | ---         |
| datetime[us]        | f64   | f64   | time        |
|---------------------|-------|-------|-------------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 00:00:00    |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 01:00:00    |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 02:00:00    |
การแปลงข้อมูลด้วย Polars

การดึงชั่วโมง

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
การแปลงข้อมูลด้วย Polars

การดึงชั่วโมง

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
| time                | price | solar | hour |
| ---                 | ---   | ---   | ---  |
| datetime[µs]        | f64   | f64   | i8   |
|---------------------|-------|-------|------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 0    |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 1    |
| 2025-07-05 12:00:00 | 0.0   | 462.0 | 12   |
การแปลงข้อมูลด้วย Polars

ส่วนประกอบ datetime อื่นๆ

  • .dt.year() - ดึงปี
  • .dt.month() - ดึงเดือน (1-12)
  • .dt.day() - ดึงวันของเดือน
  • .dt.minute() - ดึงนาที
  • ...
1 https://docs.pola.rs/api/python/dev/reference/expressions/temporal.html
การแปลงข้อมูลด้วย Polars

การปรับค่า datetime

  • .dt.offset_by() - ปรับค่า datetime
  • "1h" - เลื่อนไปข้างหน้า 1 ชั่วโมง
  • "-2d" - เลื่อนถอยหลัง 2 วัน
การแปลงข้อมูลด้วย Polars

การปรับค่า datetime

  • .dt.offset_by() - ปรับค่า datetime
  • "1h" - เลื่อนไปข้างหน้า 1 ชั่วโมง
  • "-2d" - เลื่อนถอยหลัง 2 วัน
1 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.offset_by.html
การแปลงข้อมูลด้วย Polars

การเพิ่มเวลาสิ้นสุดของช่วง

prices.with_columns(
    pl.col("time").dt.offset_by("1h")
)
การแปลงข้อมูลด้วย Polars

การเพิ่มเวลาสิ้นสุดของช่วง

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
การแปลงข้อมูลด้วย Polars

การเพิ่มเวลาสิ้นสุดของช่วง

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
| time                | price | solar | time_end            |
| ---                 | ---   | ---   | ---                 |
| datetime[µs]        | f64   | f64   | datetime[µs]        |
|---------------------|-------|-------|---------------------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 2025-07-05 01:00:00 |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 2025-07-05 02:00:00 |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 2025-07-05 03:00:00 |
การแปลงข้อมูลด้วย Polars

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

การแปลงข้อมูลด้วย Polars

Preparing Video For Download...