Pandas中的period_range()与date_range()区别

period_range与date_range的实验




1、实验背景

Pandas提供的data_range()与period_range()两个方法都可用于生成时间日期序列,给定参数主要有:

'''常用参数
sta, end, periods, freq:生成频率
'''

两个方法在使用时很容易混淆,并引发报错

2、实验过程

freq常用参数取值有:

1) freq=D:每个日历日,生成12天

d1 = pd.period_range('20220101', periods=12, freq='D')
print(d1)
'''
PeriodIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
             '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
             '2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12'],
            dtype='period[D]')
'''

d1 = pd.date_range('20220101', periods=12, freq='D')
print(d1)
'''
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
               '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
               '2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12'],
              dtype='datetime64[ns]', freq='D')
'''

2) freq=W:每周,生成6周

d2 = pd.period_range('20220101', periods=6, freq='W')
print(d2)
'''
PeriodIndex(['2021-12-27/2022-01-02', '2022-01-03/2022-01-09',
             '2022-01-10/2022-01-16', '2022-01-17/2022-01-23',
             '2022-01-24/2022-01-30', '2022-01-31/2022-02-06'],
            dtype='period[W-SUN]')
'''
d2 = pd.date_range('20220101', periods=6, freq='W')
print(d2)

'''
DatetimeIndex(['2022-01-02', '2022-01-09', '2022-01-16', '2022-01-23',
               '2022-01-30', '2022-02-06'],
              dtype='datetime64[ns]', freq='W-SUN')
'''

3) freq=M:每月末,生成6个月

d3 = pd.period_range('20220101', periods=6, freq='M')
print(d3)
'''
PeriodIndex(['2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06'], dtype='period[M]')
'''

d3 = pd.date_range('20220101', periods=6, freq='M')
print(d3)
'''
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30'],
              dtype='datetime64[ns]', freq='M')
'''

4) freq=MS:每月初,生成6个月

# period_range()报错,不支持MS频率
d4 = pd.period_range('20220101', periods=6, freq='MS')
print(d4)

d4 = pd.date_range('20220101', periods=6, freq='MS')
print(d4)
'''
DatetimeIndex(['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
               '2022-05-01', '2022-06-01'],
              dtype='datetime64[ns]', freq='MS')
'''

5) freq=B:每个工作日,生成12天

d5 = pd.period_range('20220101', periods=12, freq='B')
print(d5)
'''
PeriodIndex(['2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06',
             '2022-01-07', '2022-01-10', '2022-01-11', '2022-01-12',
             '2022-01-13', '2022-01-14', '2022-01-17', '2022-01-18'],
            dtype='period[B]')
'''

d5 = pd.date_range('20220101', periods=12, freq='B')
print(d5)
'''
DatetimeIndex(['2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06',
               '2022-01-07', '2022-01-10', '2022-01-11', '2022-01-12',
               '2022-01-13', '2022-01-14', '2022-01-17', '2022-01-18'],
              dtype='datetime64[ns]', freq='B')
'''

6) freq=Q:每个季度末,生成4个季度

d6 = pd.period_range('20220101', periods=4, freq='Q')
print(d6)
'''
PeriodIndex(['2022Q1', '2022Q2', '2022Q3', '2022Q4'], dtype='period[Q-DEC]')
'''

d6 = pd.date_range('20220101', periods=4, freq='Q')
print(d6)
'''
DatetimeIndex(['2022-03-31', '2022-06-30', '2022-09-30', '2022-12-31'], dtype='datetime64[ns]', freq='Q-DEC')
'''

7) freq=QS:每个季度初,生成4个季度

# period_range()报错,不支持QS频率
d7 = pd.period_range('20220101', periods=4, freq='QS')
print(d7)

d7 = pd.date_range('20220101', periods=4, freq='QS')
print(d7)
'''
DatetimeIndex(['2022-01-01', '2022-04-01', '2022-07-01', '2022-10-01'], dtype='datetime64[ns]', freq='QS-JAN')
'''

8) freq=Y:每个年末,生成4年

d8 = pd.period_range('20220101', periods=4, freq='Y')
print(d8)
'''
PeriodIndex(['2022', '2023', '2024', '2025'], dtype='period[A-DEC]')
'''

d8 = pd.date_range('20220101', periods=4, freq='Y')
print(d8)
'''
DatetimeIndex(['2022-12-31', '2023-12-31', '2024-12-31', '2025-12-31'], dtype='datetime64[ns]', freq='A-DEC')
'''

9) freq=YS:每个年初,生成4年

# period_range()报错,不支持YS频率
d9 = pd.period_range('20220101', periods=4, freq='YS')
print(d9)

d9 = pd.date_range('20220101', periods=4, freq='YS')
print(d9)
'''
DatetimeIndex(['2022-01-01', '2023-01-01', '2024-01-01', '2025-01-01'], dtype='datetime64[ns]', freq='AS-JAN')
'''

10) freq其他频率:H每小时频率,T每分钟频率等

3、实验结论

结论:

pd.period_range():部分频率freq参数不支持,生成的是PeriodIndex格式的日期序列

pd.date_range():频率freq参数都支持,生成的是DatetimeIndex格式的日期序列

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值