การแปลง timezone ของ Timestamp
ใช้เมธอด timestamp.tz_convert() แปลง Timestamp จาก timezone เดิมไปยัง timezone ใหม่
ขั้นตอน
- นำเข้า pandas
- สร้าง Timestamp พร้อม timezone ต้นทาง (เช่น US/Eastern)
- ใช้
tz_convert('timezoneปลายทาง')
ตัวอย่างโค้ด
import pandas as pd
# สร้าง Timestamp พร้อม timezone US/Eastern
timestamp = pd.Timestamp('2021-10-14T15:12:34.261811624', tz='US/Eastern')
# แสดง Timestamp
print("Timestamp...\n", timestamp)
# แปลง timezone เป็น Australia/Brisbane
print("\nConvert the Timestamp timezone...\n", timestamp.tz_convert('Australia/Brisbane'))
ผลลัพธ์
Timestamp... 2021-10-14 15:12:34.261811624-04:00 Convert the Timestamp timezone... 2021-10-15 05:12:34.261811624+10:00
คำอธิบาย
US/Eastern อยู่ที่ UTC-4 (วันลดเวลา) Australia/Brisbane อยู่ที่ UTC+10 ความแตกต่างคือ 14 ชั่วโมง ดังนั้นเวลาเปลี่ยนจาก 15:12 เป็น 05:12 ของวันถัดไป