Python Pandas - 获取 Period 所在月份的总天数
pythonpandasserver side programmingprogramming更新于 2026/1/2 15:08:17
要获取 Period 所在月份的总天数,请使用 period.daysinmonth 属性。
首先,导入所需的库 −
import pandas as pd
pandas.Period 表示一段时间。创建两个 Period 对象 −
period1 = pd.Period("2020-09-23")
period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2,minute = 35)
显示 Period 对象 −
print("Period1...\n", period1)
print("Period2...\n", period2)
从两个 Period 对象中获取月份中的天数 −
res1 = period1.daysinmonth res2 = period2.daysinmonth
示例
以下是代码 −
import pandas as pd
# pandas.Period 表示一段时间
# 创建两个 Period 对象
period1 = pd.Period("2020-09-23")
period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2,minute = 35)
# 显示 Period 对象
print("Period1...\n", period1)
print("Period2...\n", period2)
# 获取从两个 Period 对象中返回月份中的天数
res1 = period1.daysinmonth
res2 = period2.daysinmonth
# 从两个 Period 对象中返回月份中的天数
print("\n从第一个 Period 对象中返回月份中的天数 ...\n", res1)
print("\n从第二个 Period 对象中返回月份中的天数 ...\n", res2)
输出
将产生以下代码 −
Period1... 2020-09-23 Period2... 2021-02-14 从第一个 Period 对象中返回月份中的天数 ... 30 从第二个 Period 对象中返回月份中的天数 ... 28
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

