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

Python Pandas - แปลง Timestamp เป็น Period ด้วยความถี่รายนาที

การแปลง Timestamp เป็น Period (ความถี่รายนาที)

ใช้เมธอด timestamp.to_period(freq='T') แปลง Timestamp เป็น Period แบบรายนาที

ขั้นตอน

  1. นำเข้า pandas
  2. สร้าง Timestamp
  3. ใช้ to_period(freq='T')

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

import pandas as pd

# สร้าง Timestamp
timestamp = pd.Timestamp(2021, 9, 6, 11, 50, 20, 33)

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

# แปลงเป็น Period ความถี่รายนาที
print("\nTimestamp to Period...\n", timestamp.to_period(freq='T'))

ผลลัพธ์

Timestamp... 2021-09-06 11:50:20.000033
Timestamp to Period... 2021-09-06 11:50

คำอธิบาย

ความถี่ 'T' (Minutely) ปัดเศษวินาทีและส่วนทศนิยมทิ้ง คงเหลือถึงระดับนาที