Pandas2.2 Index objects
PeriodIndex Properties
| 方法 | 描述 |
|---|---|
| PeriodIndex.day | 用于获取 PeriodIndex 中每个周期对应的日(day)组件 |
| PeriodIndex.dayofweek | 用于获取 PeriodIndex 中每个周期对应的星期几 |
| PeriodIndex.day_of_week | 用于获取 PeriodIndex 中每个周期对应日期的星期几(数值形式) |
| PeriodIndex.dayofyear | 用于获取 PeriodIndex 中每个周期对应日期在一年中的天数(序数日期) |
| PeriodIndex.day_of_year | 用于获取 PeriodIndex 中每个周期对应日期在一年中的天数(序数日期) |
| PeriodIndex.days_in_month | 用于获取 PeriodIndex 中每个周期对应月份的天数 |
| PeriodIndex.daysinmonth | 用于获取 PeriodIndex 中每个周期对应月份的天数 |
pandas.PeriodIndex.daysinmonth
方法描述
pandas.PeriodIndex.daysinmonth 是 PeriodIndex 对象的一个属性,用于获取 PeriodIndex 中每个周期对应月份的天数。该属性返回一个 Index 对象,包含每个周期所在月份的总天数。
返回值
返回一个 Index 对象,包含每个周期所在月份的天数:
- 28、29 天表示二月(平年或闰年)
- 30 天表示 4、6、9、11 月
- 31 天表示 1、3、5、7、8、10、12 月
使用示例
import pandas as pd
# 创建一个包含不同月份的 PeriodIndex
periods_monthly = pd.period_range('2023-01', periods=12, freq='M')
print("原始 PeriodIndex:")
print(periods_monthly)
print()
# 获取每个月份的天数
days_in_month_values = periods_monthly.daysinmonth
print("每个月份的天数:")
print(days_in_month_values)
print()
# 创建包含更多信息的 DataFrame 来展示实际用途
df = pd.DataFrame({
'period': periods_monthly,
'days_in_month': days_in_month_values
})
print("完整的数据展示:")
print(df)
执行结果:
原始 PeriodIndex:
PeriodIndex(['2023-01', '2023-02', '2023-03', '2023-04', '2023-05', '2023-06',
'2023-07', '2023-08', '2023-09', '2023-10', '2023-11', '2023-12'],
dtype='period[M]')
每个月份的天数:
Int64Index([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], dtype='int64')
完整的数据展示:
period days_in_month
0 2023-01 31
1 2023-02 28
2 2023-03 31
3 2023-04 30
4 2023-05 31
5 2023-06 30
6 2023-07 31
7 2023-08 31
8 2023-09 30
9 2023-10 31
10 2023-11 30
11 2023-12 31
闰年处理示例
import pandas as pd
# 创建包含闰年二月的 PeriodIndex
leap_year_periods = pd.period_range('2024-01', periods=12, freq='M')
print("闰年 PeriodIndex:")
print(leap_year_periods)
print()
print("每个月份的天数:")
print(leap_year_periods.daysinmonth)
print()
# 对比非闰年
non_leap_year_periods = pd.period_range('2023-01', periods=12, freq='M')
print("非闰年 PeriodIndex:")
print(non_leap_year_periods)
print()
print("每个月份的天数:")
print(non_leap_year_periods.daysinmonth)
执行结果:
闰年 PeriodIndex:
PeriodIndex(['2024-01', '2024-02', '2024-03', '2024-04', '2024-05', '2024-06',
'2024-07', '2024-08', '2024-09', '2024-10', '2024-11', '2024-12'],
dtype='period[M]')
每个月份的天数:
Int64Index([31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], dtype='int64')
非闰年 PeriodIndex:
PeriodIndex(['2023-01', '2023-02', '2023-03', '2023-04', '2023-05', '2023-06',
'2023-07', '2023-08', '2023-09', '2023-10', '2023-11', '2023-12'],
dtype='period[M]')
每个月份的天数:
Int64Index([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], dtype='int64')
日频率下的使用示例
import pandas as pd
# 创建一个日频率的 PeriodIndex,跨越不同月份
daily_periods = pd.period_range('2023-01-30', periods=5, freq='D')
print("日频率 PeriodIndex:")
print(daily_periods)
print()
# 获取每一天所在月份的天数
days_in_month_daily = daily_periods.daysinmonth
print("每一天所在月份的天数:")
print(days_in_month_daily)
print()
# 创建包含更多信息的 DataFrame 来展示实际用途
df_daily = pd.DataFrame({
'period': daily_periods,
'days_in_month': days_in_month_daily
})
print("完整的数据展示:")
print(df_daily)
执行结果:
日频率 PeriodIndex:
PeriodIndex(['2023-01-30', '2023-01-31', '2023-02-01', '2023-02-02',
'2023-02-03'],
dtype='period[D]')
每一天所在月份的天数:
Int64Index([31, 31, 28, 28, 28], dtype='int64')
完整的数据展示:
period days_in_month
0 2023-01-30 31
1 2023-01-31 31
2 2023-02-01 28
3 2023-02-02 28
4 2023-02-03 28
实际应用场景
import pandas as pd
import numpy as np
# 模拟一年的销售数据,按月记录
dates = pd.period_range('2023-01', periods=12, freq='M')
sales = np.random.randint(10000, 50000, size=12) # 随机月销售额
sales_data = pd.DataFrame({
'month': dates,
'sales': sales
})
# 添加每月天数信息
sales_data['days_in_month'] = sales_data['month'].dt.daysinmonth
# 计算日均销售额
sales_data['daily_average'] = sales_data['sales'] / sales_data['days_in_month']
print("销售数据与每月天数:")
print(sales_data)
print()
# 找出日均销售额最高的月份
highest_daily_avg = sales_data.loc[sales_data['daily_average'].idxmax()]
print(f"日均销售额最高的月份: {highest_daily_avg['month']}")
print(f"该月销售额: {highest_daily_avg['sales']}")
print(f"该月天数: {highest_daily_avg['days_in_month']}")
print(f"日均销售额: {highest_daily_avg['daily_average']:.2f}")
不同频率下的使用
import pandas as pd
# 季度频率
quarterly_periods = pd.period_range('2023Q1', periods=4, freq='Q')
print("季度频率 PeriodIndex:")
print(quarterly_periods)
print("每个季度结束月份的天数:")
print(quarterly_periods.daysinmonth)
print()
# 年频率
yearly_periods = pd.period_range('2020', periods=5, freq='Y')
print("年频率 PeriodIndex:")
print(yearly_periods)
print("每年12月的天数:")
print(yearly_periods.daysinmonth)
执行结果:
季度频率 PeriodIndex:
PeriodIndex(['2023Q1', '2023Q2', '2023Q3', '2023Q4'], dtype='period[Q-DEC]')
每个季度结束月份的天数:
Int64Index([31, 30, 30, 31], dtype='int64')
年频率 PeriodIndex:
PeriodIndex(['2020', '2021', '2022', '2023', '2024'], dtype='period[A-DEC]')
每年12月的天数:
Int64Index([31, 31, 31, 31, 31], dtype='int64')
注意事项
daysinmonth属性适用于所有频率的 PeriodIndex,包括年、季度、月、日等频率- 对于月频率,返回的是该月的总天数
- 对于日频率,返回的是该日所在月份的总天数
- 对于季度频率,返回的是该季度最后一个月的天数
- 对于年频率,返回的是该年12月的天数(因为年周期默认以12月结束)
- 闰年处理:闰年的二月会正确返回29天,平年二月返回28天
- 该属性是只读的,不能修改
17万+

被折叠的 条评论
为什么被折叠?



