Python Pandas - 獲取週期所在月份的天數


要獲取週期所在的月份中的天數,請使用 period.day 屬性。

首先,匯入所需的庫 -

import pandas as pd

pandas.Period 表示一段時間。建立兩個週期物件 -

period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)

從兩個週期物件獲取當月的天數 -

res1 = period1.day
res2 = period2.day

示例

以下為程式碼 -

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)

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

# get the day of the month from two Period objects
res1 = period1.day
res2 = period2.day

# Return the day of the month from the two Period objects
print("\nDay of the month from the 1st Period object ...\n", res1)
print("\nDay of the month from the 2nd Period object...\n", res2)

輸出

將生成以下程式碼 -

Period1...
2021-09-18
Period2...
2021-09-22

Day of the month from the 1st Period object ...
18

Day of the month from the 2nd Period object...
22

更新於: 2021 年 10 月 14 日

175 瀏覽量

開啟您的職業生涯

完成課程獲取證書

開始學習
Advertisement Advertisements
© . All rights reserved.