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

示例

以下为代码 -

Open Compiler
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