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

Pandas - แปลง Timestamp เป็น native Python datetime object

การแปลง Pandas Timestamp เป็น Python datetime

ใช้เมธอด timestamp.to_pydatetime() แปลง Timestamp ของ Pandas ให้เป็น datetime object มาตรฐานของ Python

ขั้นตอน

  1. นำเข้า pandas
  2. สร้าง Timestamp
  3. ใช้ to_pydatetime()

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

import pandas as pd

# สร้าง Timestamp
timestamp = pd.Timestamp('2021-09-11T13:12:34.261811')

# แสดง Timestamp
print("Timestamp...\n", timestamp)

# แปลงเป็น native Python datetime
print("\nConvert Timestamp...\n", timestamp.to_pydatetime())

ผลลัพธ์

Timestamp... 2021-09-11 13:12:34.261811
Convert Timestamp... 2021-09-11 13:12:34.261811

ประโยชน์

เมื่อต้องการใช้งานกับไลบรารีอื่นที่รับเฉพาะ datetime ของ Python มาตรฐาน