Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - ดึงเขตเวลาจาก DateTimeIndex ด้วย tz property


คำอธิบาย

ใช้ property DateTimeIndex.tz เพื่อดึงข้อมูลเขตเวลา (timezone) ของ DateTimeIndex

ตัวอย่างโค้ด

import pandas as pd

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

print("DateTimeIndex...\n", datetimeindex)
print("\nGet the timezone..\n", datetimeindex.tz)

ผลลัพธ์

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-21 02:30:50+11:00',
               '2021-10-22 02:30:50+11:00', '2021-10-23 02:30:50+11:00',
               '2021-10-24 02:30:50+11:00', '2021-10-25 02:30:50+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='D')

Get the timezone..
Australia/Sydney

หมายเหตุ

  • คืนค่า tzinfo object (pytz หรือ dateutil timezone)
  • ใช้สำหรับตรวจสอบ/แปลงเขตเวลา
  • DateTimeIndex ต้องสร้างด้วย tz parameter