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

Python Pandas - คืน Timedelta ใหม่ที่ปัดลง (Floor) ตามความละเอียดรายนาที

เพื่อคืน Timedelta ใหม่ที่ปัดลง (floor) ตามความละเอียดที่ระบุ ให้ใช้เมธอด timedelta.floor() สำหรับความละเอียดปัดลงตามนาที ให้ตั้งค่าพารามิเตอร์ freq เป็น 'T'

เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็น:

import pandas as pd

TimeDeltas เป็นไลบรารี datetime มาตรฐานของ Python ใช้การแทนค่าที่ต่างกัน สร้าง Timedelta object

timedelta = pd.Timedelta('8 days 11 hours 39 min 18 s')

แสดง Timedelta

print("Timedelta...\n", timedelta)

คืนค่า Timestamp ที่ปัดลงแล้วด้วยความละเอียดรายนาที:

timedelta.floor(freq='T')

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

import pandas as pd

# TimeDeltas is Python's standard datetime library uses a different representation timedelta's
# create a Timedelta object
timedelta = pd.Timedelta('8 days 11 hours 39 min 18 s')

# display the Timedelta
print("Timedelta...\n", timedelta)

# return the floored Timestamp
# with minutely floored resolution
res = timedelta.floor(freq='T')

# display the floored Timestamp
print("\nTimedelta (minutely floored)...\n", res)

เอาต์พุต

จะได้ผลลัพธ์ดังนี้:

Timedelta... 8 days 11:39:18
Timedelta (minutely floored)... 8 days 11:39:00