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

Python Pandas - รับองค์ประกอบที่สองของช่วงเวลา


เพื่อให้ได้องค์ประกอบที่สองของช่วงเวลา ให้ใช้ period.second คุณสมบัติ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

pandas.Period หมายถึงช่วงเวลาหนึ่ง การสร้างวัตถุสองช่วงเวลา

period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="S", year = 2021, month = 7, day = 16, hour = 2, minute = 35, second = 10)

แสดงวัตถุรอบระยะเวลา

print("Period1...\n", period1)
print("Period2...\n", period2)

รับวินาทีจากวัตถุสองช่วงเวลา

res1 = period1.second
res2 = period2.second

ตัวอย่าง

ต่อไปนี้เป็นรหัส

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="S", year = 2021, month = 7, day = 16, hour = 2, minute = 35, second = 10)

# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

# get the seconds from two Period objects
res1 = period1.second
res2 = period2.second

# Return the seconds from the two Period objects
print("\nSeconds component from the 1st Period object ...\n", res1)
print("\nSeconds component from the 1st Period object ...\n", res2)

ผลลัพธ์

ซึ่งจะได้รหัสดังต่อไปนี้

Period1...
2020-09-23 05:55:30
Period2...
2021-07-16 02:35:10

Seconds component from the 1st Period object ...
30

Seconds component from the 1st Period object ...
10