pd.date_range()-日期范围:生成日期范围
- 2种生成方式:①start + end; ②start/end + periods
- 默认频率:day
- 直接生成DatetimeIndex
pd.date_range(start=None, end=None, periods=None, freq=‘D’, tz=None, normalize=False, name=None, closed=None, **kwargs)
- start:开始时间
- end:结束时间
- periods:偏移量
- freq:频率,默认天,pd.date_range()默认频率为日历日,pd.bdate_range()默认频率为工作日
- tz:时区
- name:索引对象名称
一、生成方式
生成方式:
- start + end;
- start + periods
- end + periods
import pandas as pd
# pd.date_range()-日期范围:生成日期范围
# 2种生成方式:①start + end; ②start/end + periods
# 默认频率:day
# 直接生成DatetimeIndex
rng1 = pd.date_range(start='1/1/2017', end='1/10/2017', normalize=True)
rng2 = pd.date_range(start='1/1/2017', periods=10)
rng3 = pd.date_range(end='1/30/2017 15:00:00', periods=10) # 增加了时、分、秒
print("rng1 = {0}, type(rng1) = {1}".format(rng1, type(rng1)))
print('-' * 200)
print("rng2 = {0}, type(rng2) = {1}".format(rng2, type(rng2)))
print('-' * 200)
print("rng3 = {0}, type(rng3) = {1}".format(rng3, type(rng3)))
print('-' * 200)
打印结果:
rng1 = DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
'2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
'2017-01-09', '2017-01-10'],
dtype='datetime64[ns]', freq='D'), type(rng1) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rng2 = DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
'2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
'2017-01-09', '2017-01-10'],
dtype='datetime64[ns]', freq='D'), type(rng2) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rng3 = DatetimeIndex(['2017-01-21 15:00:00', '2017-01-22 15:00:00',
'2017-01-23 15:00:00', '2017-01-24 15:00:00',
'2017-01-25 15:00:00', '2017-01-26 15:00:00',
'2017-01-27 15:00:00', '2017-01-28 15:00:00',
'2017-01-29 15:00:00', '2017-01-30 15:00:00'],
dtype='datetime64[ns]', freq='D'), type(rng3) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Process finished with exit code 0
二、normalize参数
normalize:时间参数值正则化到午夜时间戳(这里最后就直接变成0:00:00,并不是15:30:00)
import pandas as pd
# pd.date_range()-日期范围:生成日期范围
# 2种生成方式:①start + end; ②start/end + periods
# 默认频率:day
# normalize:时间参数值正则化到午夜时间戳(这里最后就直接变成0:00:00,并不是15:30:00)
rng4 = pd.date_range(start='1/1/2017 15:30', periods=3, name='hello world!', normalize=True)
print("rng4 = {0}, type(rng4) = {1}".format(rng4, type(rng4)))
print('-' * 200)
打印结果:
rng4 = DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'], dtype='datetime64[ns]', name='hello world!', freq='D'), type(rng4) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
三、closed参数
closed:默认为None的情况下,左闭右闭,left则左闭右开,right则左开右闭
import pandas as pd
# pd.date_range()-日期范围:生成日期范围
# 2种生成方式:①start + end; ②start/end + periods
# 默认频率:day
# closed:默认为None的情况下,左闭右闭,left则左闭右开,right则左开右闭
rng5 = pd.date_range('20170101', '20170104') # 20170101也可读取
rng6 = pd.date_range('20170101', '20170104', closed='right')
rng7 = pd.date_range('20170101', '20170104', closed='left')
print("rng5 = {0}, type(rng5) = {1}".format(rng5, type(rng5)))
print("rng6 = {0}, type(rng6) = {1}".format(rng6, type(rng6)))
print("rng7 = {0}, type(rng7) = {1}".format(rng7, type(rng7)))
print('-' * 200)
打印结果:
rng5 = DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D'), type(rng5) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
rng6 = DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D'), type(rng6) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
rng7 = DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'], dtype='datetime64[ns]', freq='D'), type(rng7) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
四、pd.bdate_range():默认频率为工作日
import pandas as pd
# pd.bdate_range():生成日期范围【默认频率为工作日】
rng8 = pd.bdate_range('20170101', '20170105')
print("rng8 = {0}, type(rng8) = {1}".format(rng8, type(rng8)))
print('-' * 200)
打印结果:
rng8 = DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05'], dtype='datetime64[ns]', freq='B'), type(rng8) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
五、DatetimeIndex类型转为list,元素为Timestamp类型
直接转化为list,元素为Timestamp类型
import pandas as pd
# pd.date_range()-日期范围:生成日期范围
rng9 = pd.date_range('20170101', '20170105')
print("rng9 = {0}, type(rng9) = {1}".format(rng9, type(rng9)))
print('-' * 150)
# 直接转化为list,元素为Timestamp
li = list(rng9)
print("li = ", li)
打印结果:
rng9 = DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05'],
dtype='datetime64[ns]', freq='D'), type(rng9) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
------------------------------------------------------------------------------------------------------------------------------------------------------
li = [Timestamp('2017-01-01 00:00:00', freq='D'), Timestamp('2017-01-02 00:00:00', freq='D'), Timestamp('2017-01-03 00:00:00', freq='D'), Timestamp('2017-01-04 00:00:00', freq='D'), Timestamp('2017-01-05 00:00:00', freq='D')]
Process finished with exit code 0
六、频率参数:freq
默认freq = ‘D’:每日历日
- B:每工作日
- H**:**每小时
- T/MIN:每分
- S:每秒
- L:每毫秒(千分之一秒)
- U:每微秒(百万分之一秒)
- W-MON:从指定星期几开始算起,每周
- 星期几缩写:MON/TUE/WED/THU/FRI/SAT/SUN
- WOM-2MON:每月的第几个星期几开始算,这里是每月第二个星期一
- M:每月最后一个日历日
- MS:每月第一个日历日
- Q-月:指定月为季度末,每个季度末最后一月的最后一个日历日
- 月缩写:JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC
- 所以Q-月只有三种情况:1-4-7-10,2-5-8-11,3-6-9-12
- QS-月:指定月为季度末,每个季度末最后一月的第一个日历日
- A-月:每年指定月份的最后一个日历日
- AS-月:每年指定月份的第一个日历日
- BM:每月最后一个工作日
- BMS:每月第一个工作日
- BQ-月:指定月为季度末,每个季度末最后一月的最后一个工作日
- BQS-月:指定月为季度末,每个季度末最后一月的第一个工作日
- BA-月:每年指定月份的最后一个工作日
- BAS-月:每年指定月份的第一个工作日
import numpy as np
import pandas as pd
# pd.date_range()-日期范围:频率参数
# 默认freq = 'D':每日历日
print("pd.date_range('2017/1/1', '2017/1/4') = \n", pd.date_range('2017/1/1', '2017/1/4'))
print("-" * 100)
# B:每工作日
print("pd.date_range('2017/1/1', '2017/1/4', freq='B') = \n", pd.date_range('2017/1/1', '2017/1/4', freq='B'))
print("-" * 100)
# H:每小时
print("pd.date_range('2017/1/1', '2017/1/2', freq='H') = \n", pd.date_range('2017/1/1', '2017/1/2', freq='H'))
print("-" * 100)
# T/MIN:每分
print("pd.date_range('2017/1/1 12:00', '2017/1/1 12:10', freq='T') = \n", pd.date_range('2017/1/1 12:00', '2017/1/1 12:10', freq='T'))
print("-" * 100)
# S:每秒
print("pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='S') = \n", pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='S'))
print("-" * 100)
# L:每毫秒(千分之一秒)
print("pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='L') = \n", pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='L'))
print("-" * 100)
# U:每微秒(百万分之一秒)
print("pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='U') = \n", pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='U'))
print("-" * 200)
# W-MON:从指定星期几开始算起,每周
# 星期几缩写:MON/TUE/WED/THU/FRI/SAT/SUN
print("pd.date_range('2017/1/1', '2017/2/1', freq='W-MON') = \n", pd.date_range('2017/1/1', '2017/2/1', freq='W-MON'))
# WOM-2MON:每月的第几个星期几开始算,这里是每月第二个星期一
print("pd.date_range('2017/1/1', '2017/5/1', freq='WOM-2MON') = \n", pd.date_range('2017/1/1', '2017/5/1', freq='WOM-2MON'))
print("-" * 200)
# M:每月最后一个日历日
print("pd.date_range('2017', '2018', freq='M') = \n", pd.date_range('2017', '2018', freq='M'))
print("-" * 100)
# Q-月:指定月为季度末,每个季度末最后一月的最后一个日历日
# 月缩写:JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC
# 所以Q-月只有三种情况:1-4-7-10,2-5-8-11,3-6-9-12
print("pd.date_range('2017', '2020', freq='Q-DEC') = \n", pd.date_range('2017', '2020', freq='Q-DEC'))
print("-" * 100)
# A-月:每年指定月份的最后一个日历日
print("pd.date_range('2017', '2020', freq='A-DEC') = \n", pd.date_range('2017', '2020', freq='A-DEC'))
print("-" * 200)
# BM:每月最后一个工作日
print("pd.date_range('2017', '2018', freq='BM') = \n", pd.date_range('2017', '2018', freq='BM'))
print("-" * 100)
# BQ-月:指定月为季度末,每个季度末最后一月的最后一个工作日
print("pd.date_range('2017', '2020', freq='BQ-DEC') = \n", pd.date_range('2017', '2020', freq='BQ-DEC'))
print("-" * 100)
# BA-月:每年指定月份的最后一个工作日
print("pd.date_range('2017', '2020', freq='BA-DEC') = \n", pd.date_range('2017', '2020', freq='BA-DEC'))
print("-" * 200)
# MS:每月第一个日历日
print("pd.date_range('2017', '2018', freq='MS') = \n", pd.date_range('2017', '2018', freq='MS'))
print("-" * 100)
# QS-月:指定月为季度末,每个季度末最后一月的第一个日历日
print("pd.date_range('2017', '2020', freq='QS-DEC') = \n", pd.date_range('2017', '2020', freq='QS-DEC'))
print("-" * 100)
# AS-月:每年指定月份的第一个日历日
print("pd.date_range('2017', '2020', freq='AS-DEC') = \n", pd.date_range('2017', '2020', freq='AS-DEC'))
print("-" * 200)
# BMS:每月第一个工作日
print("pd.date_range('2017', '2018', freq='BMS') = \n", pd.date_range('2017', '2018', freq='BMS'))
print("-" * 100)
# BQS-月:指定月为季度末,每个季度末最后一月的第一个工作日
print("pd.date_range('2017', '2020', freq='BQS-DEC') = \n", pd.date_range('2017', '2020', freq='BQS-DEC'))
print("-" * 100)
# BAS-月:每年指定月份的第一个工作日
print("pd.date_range('2017', '2020', freq='BAS-DEC') = \n", pd.date_range('2017', '2020', freq='BAS-DEC'))
打印结果:
pd.date_range('2017/1/1', '2017/1/4') =
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1', '2017/1/4', freq='B') =
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='B')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1', '2017/1/2', freq='H') =
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 01:00:00',
'2017-01-01 02:00:00', '2017-01-01 03:00:00',
'2017-01-01 04:00:00', '2017-01-01 05:00:00',
'2017-01-01 06:00:00', '2017-01-01 07:00:00',
'2017-01-01 08:00:00', '2017-01-01 09:00:00',
'2017-01-01 10:00:00', '2017-01-01 11:00:00',
'2017-01-01 12:00:00', '2017-01-01 13:00:00',
'2017-01-01 14:00:00', '2017-01-01 15:00:00',
'2017-01-01 16:00:00', '2017-01-01 17:00:00',
'2017-01-01 18:00:00', '2017-01-01 19:00:00',
'2017-01-01 20:00:00', '2017-01-01 21:00:00',
'2017-01-01 22:00:00', '2017-01-01 23:00:00',
'2017-01-02 00:00:00'],
dtype='datetime64[ns]', freq='H')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1 12:00', '2017/1/1 12:10', freq='T') =
DatetimeIndex(['2017-01-01 12:00:00', '2017-01-01 12:01:00',
'2017-01-01 12:02:00', '2017-01-01 12:03:00',
'2017-01-01 12:04:00', '2017-01-01 12:05:00',
'2017-01-01 12:06:00', '2017-01-01 12:07:00',
'2017-01-01 12:08:00', '2017-01-01 12:09:00',
'2017-01-01 12:10:00'],
dtype='datetime64[ns]', freq='T')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='S') =
DatetimeIndex(['2017-01-01 12:00:00', '2017-01-01 12:00:01',
'2017-01-01 12:00:02', '2017-01-01 12:00:03',
'2017-01-01 12:00:04', '2017-01-01 12:00:05',
'2017-01-01 12:00:06', '2017-01-01 12:00:07',
'2017-01-01 12:00:08', '2017-01-01 12:00:09',
'2017-01-01 12:00:10'],
dtype='datetime64[ns]', freq='S')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='L') =
DatetimeIndex([ '2017-01-01 12:00:00', '2017-01-01 12:00:00.001000',
'2017-01-01 12:00:00.002000', '2017-01-01 12:00:00.003000',
'2017-01-01 12:00:00.004000', '2017-01-01 12:00:00.005000',
'2017-01-01 12:00:00.006000', '2017-01-01 12:00:00.007000',
'2017-01-01 12:00:00.008000', '2017-01-01 12:00:00.009000',
...
'2017-01-01 12:00:09.991000', '2017-01-01 12:00:09.992000',
'2017-01-01 12:00:09.993000', '2017-01-01 12:00:09.994000',
'2017-01-01 12:00:09.995000', '2017-01-01 12:00:09.996000',
'2017-01-01 12:00:09.997000', '2017-01-01 12:00:09.998000',
'2017-01-01 12:00:09.999000', '2017-01-01 12:00:10'],
dtype='datetime64[ns]', length=10001, freq='L')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1 12:00:00', '2017/1/1 12:00:10', freq='U') =
DatetimeIndex([ '2017-01-01 12:00:00', '2017-01-01 12:00:00.000001',
'2017-01-01 12:00:00.000002', '2017-01-01 12:00:00.000003',
'2017-01-01 12:00:00.000004', '2017-01-01 12:00:00.000005',
'2017-01-01 12:00:00.000006', '2017-01-01 12:00:00.000007',
'2017-01-01 12:00:00.000008', '2017-01-01 12:00:00.000009',
...
'2017-01-01 12:00:09.999991', '2017-01-01 12:00:09.999992',
'2017-01-01 12:00:09.999993', '2017-01-01 12:00:09.999994',
'2017-01-01 12:00:09.999995', '2017-01-01 12:00:09.999996',
'2017-01-01 12:00:09.999997', '2017-01-01 12:00:09.999998',
'2017-01-01 12:00:09.999999', '2017-01-01 12:00:10'],
dtype='datetime64[ns]', length=10000001, freq='U')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1', '2017/2/1', freq='W-MON') =
DatetimeIndex(['2017-01-02', '2017-01-09', '2017-01-16', '2017-01-23', '2017-01-30'],
dtype='datetime64[ns]', freq='W-MON')
pd.date_range('2017/1/1', '2017/5/1', freq='WOM-2MON') =
DatetimeIndex(['2017-01-09', '2017-02-13', '2017-03-13', '2017-04-10'], dtype='datetime64[ns]', freq='WOM-2MON')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pd.date_range('2017', '2018', freq='M') =
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30',
'2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
'2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31'],
dtype='datetime64[ns]', freq='M')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='Q-DEC') =
DatetimeIndex(['2017-03-31', '2017-06-30', '2017-09-30', '2017-12-31',
'2018-03-31', '2018-06-30', '2018-09-30', '2018-12-31',
'2019-03-31', '2019-06-30', '2019-09-30', '2019-12-31'],
dtype='datetime64[ns]', freq='Q-DEC')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='A-DEC') =
DatetimeIndex(['2017-12-31', '2018-12-31', '2019-12-31'], dtype='datetime64[ns]', freq='A-DEC')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pd.date_range('2017', '2018', freq='BM') =
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-28',
'2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
'2017-09-29', '2017-10-31', '2017-11-30', '2017-12-29'],
dtype='datetime64[ns]', freq='BM')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='BQ-DEC') =
DatetimeIndex(['2017-03-31', '2017-06-30', '2017-09-29', '2017-12-29',
'2018-03-30', '2018-06-29', '2018-09-28', '2018-12-31',
'2019-03-29', '2019-06-28', '2019-09-30', '2019-12-31'],
dtype='datetime64[ns]', freq='BQ-DEC')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='BA-DEC') =
DatetimeIndex(['2017-12-29', '2018-12-31', '2019-12-31'], dtype='datetime64[ns]', freq='BA-DEC')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pd.date_range('2017', '2018', freq='MS') =
DatetimeIndex(['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01',
'2017-05-01', '2017-06-01', '2017-07-01', '2017-08-01',
'2017-09-01', '2017-10-01', '2017-11-01', '2017-12-01',
'2018-01-01'],
dtype='datetime64[ns]', freq='MS')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='QS-DEC') =
DatetimeIndex(['2017-03-01', '2017-06-01', '2017-09-01', '2017-12-01',
'2018-03-01', '2018-06-01', '2018-09-01', '2018-12-01',
'2019-03-01', '2019-06-01', '2019-09-01', '2019-12-01'],
dtype='datetime64[ns]', freq='QS-DEC')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='AS-DEC') =
DatetimeIndex(['2017-12-01', '2018-12-01', '2019-12-01'], dtype='datetime64[ns]', freq='AS-DEC')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pd.date_range('2017', '2018', freq='BMS') =
DatetimeIndex(['2017-01-02', '2017-02-01', '2017-03-01', '2017-04-03',
'2017-05-01', '2017-06-01', '2017-07-03', '2017-08-01',
'2017-09-01', '2017-10-02', '2017-11-01', '2017-12-01',
'2018-01-01'],
dtype='datetime64[ns]', freq='BMS')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='BQS-DEC') =
DatetimeIndex(['2017-03-01', '2017-06-01', '2017-09-01', '2017-12-01',
'2018-03-01', '2018-06-01', '2018-09-03', '2018-12-03',
'2019-03-01', '2019-06-03', '2019-09-02', '2019-12-02'],
dtype='datetime64[ns]', freq='BQS-DEC')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2020', freq='BAS-DEC') =
DatetimeIndex(['2017-12-01', '2018-12-03', '2019-12-02'], dtype='datetime64[ns]', freq='BAS-DEC')
Process finished with exit code 0
七、复合频率
import pandas as pd
# 7天
print("pd.date_range('2017/1/1', '2017/2/1', freq='7D') = \n", pd.date_range('2017/1/1', '2017/2/1', freq='7D'))
print("-" * 100)
# 2小时30分钟
print("pd.date_range('2017/1/1', '2017/1/2', freq='2h30min') = \n", pd.date_range('2017/1/1', '2017/1/2', freq='2h30min'))
print("-" * 100)
# 2月,每月最后一个日历日
print("pd.date_range('2017', '2018', freq='2M') = \n", pd.date_range('2017', '2018', freq='2M'))
print("-" * 100)
打印结果:
pd.date_range('2017/1/1', '2017/2/1', freq='7D') =
DatetimeIndex(['2017-01-01', '2017-01-08', '2017-01-15', '2017-01-22',
'2017-01-29'],
dtype='datetime64[ns]', freq='7D')
----------------------------------------------------------------------------------------------------
pd.date_range('2017/1/1', '2017/1/2', freq='2h30min') =
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 02:30:00',
'2017-01-01 05:00:00', '2017-01-01 07:30:00',
'2017-01-01 10:00:00', '2017-01-01 12:30:00',
'2017-01-01 15:00:00', '2017-01-01 17:30:00',
'2017-01-01 20:00:00', '2017-01-01 22:30:00'],
dtype='datetime64[ns]', freq='150T')
----------------------------------------------------------------------------------------------------
pd.date_range('2017', '2018', freq='2M') =
DatetimeIndex(['2017-01-31', '2017-03-31', '2017-05-31', '2017-07-31',
'2017-09-30', '2017-11-30'],
dtype='datetime64[ns]', freq='2M')
----------------------------------------------------------------------------------------------------
Process finished with exit code 0
八、频率转换
import numpy as np
import pandas as pd
# 改变频率,这里是D改为4H
# method:插值模式,None不插值,ffill用之前值填充,bfill用之后值填充
ts = pd.Series(np.random.rand(4), index=pd.date_range('20170101', '20170104'))
print("ts = \n", ts)
print("-" * 100)
print("ts.asfreq('4H', method='ffill') = \n", ts.asfreq('4H', method='ffill'))
打印结果:
ts =
2017-01-01 0.695553
2017-01-02 0.036298
2017-01-03 0.316031
2017-01-04 0.757398
Freq: D, dtype: float64
----------------------------------------------------------------------------------------------------
ts.asfreq('4H', method='ffill') =
2017-01-01 00:00:00 0.695553
2017-01-01 04:00:00 0.695553
2017-01-01 08:00:00 0.695553
2017-01-01 12:00:00 0.695553
2017-01-01 16:00:00 0.695553
2017-01-01 20:00:00 0.695553
2017-01-02 00:00:00 0.036298
2017-01-02 04:00:00 0.036298
2017-01-02 08:00:00 0.036298
2017-01-02 12:00:00 0.036298
2017-01-02 16:00:00 0.036298
2017-01-02 20:00:00 0.036298
2017-01-03 00:00:00 0.316031
2017-01-03 04:00:00 0.316031
2017-01-03 08:00:00 0.316031
2017-01-03 12:00:00 0.316031
2017-01-03 16:00:00 0.316031
2017-01-03 20:00:00 0.316031
2017-01-04 00:00:00 0.757398
Freq: 4H, dtype: float64
----------------------------------------------------------------------------------------------------
Process finished with exit code 0
九、超前/滞后数据(对数值进行位移)
import numpy as np
import pandas as pd
# 日期范围:超前/滞后数据
ts = pd.Series(np.random.rand(4), index=pd.date_range('20170101', '20170104'))
print("ts = \n", ts)
print("-" * 100)
# 正数:数值后移(滞后)
ts1 = ts.shift(2)
# 负数:数值前移(超前)
ts2 = ts.shift(-2)
print("ts1 = ts.shift(2) = \n", ts1)
print("-" * 100)
print("ts2 = ts.shift(-2) = \n", ts2)
print("-" * 200)
# 计算变化百分比,这里计算:该时间戳与上一个时间戳相比,变化百分比
print("ts = \n", ts)
print("-" * 50)
ts3 = ts.shift(1)
print("ts3 = ts.shift(1) = \n", ts3)
print("-" * 100)
print("ts / ts3 = \n", ts / ts3)
print("-" * 50)
per = ts / ts3 - 1
print("per = ts / ts3 - 1 = \n", per)
print("-" * 200)
打印结果:
ts =
2017-01-01 0.727595
2017-01-02 0.979893
2017-01-03 0.489282
2017-01-04 0.540695
Freq: D, dtype: float64
----------------------------------------------------------------------------------------------------
ts1 = ts.shift(2) =
2017-01-01 NaN
2017-01-02 NaN
2017-01-03 0.727595
2017-01-04 0.979893
Freq: D, dtype: float64
----------------------------------------------------------------------------------------------------
ts2 = ts.shift(-2) =
2017-01-01 0.489282
2017-01-02 0.540695
2017-01-03 NaN
2017-01-04 NaN
Freq: D, dtype: float64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ts =
2017-01-01 0.727595
2017-01-02 0.979893
2017-01-03 0.489282
2017-01-04 0.540695
Freq: D, dtype: float64
--------------------------------------------------
ts3 = ts.shift(1) =
2017-01-01 NaN
2017-01-02 0.727595
2017-01-03 0.979893
2017-01-04 0.489282
Freq: D, dtype: float64
----------------------------------------------------------------------------------------------------
ts / ts3 =
2017-01-01 NaN
2017-01-02 1.346756
2017-01-03 0.499322
2017-01-04 1.105080
Freq: D, dtype: float64
--------------------------------------------------
per = ts / ts3 - 1 =
2017-01-01 NaN
2017-01-02 0.346756
2017-01-03 -0.500678
2017-01-04 0.105080
Freq: D, dtype: float64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
十、对时间戳进行位移(不是对数值进行位移)
import numpy as np
import pandas as pd
# 日期范围:超前/滞后数据
ts = pd.Series(np.random.rand(4), index=pd.date_range('20170101', '20170104'))
print("ts = \n", ts)
print("-" * 100)
# 加上freq参数:对时间戳进行位移,而不是对数值进行位移
print("ts.shift(2, freq='D') = \n", ts.shift(2, freq='D'))
print("-" * 50)
print("ts.shift(2, freq='T') = \n", ts.shift(2, freq='T'))
print("-" * 200)
打印结果:
ts =
2017-01-01 0.038605
2017-01-02 0.074905
2017-01-03 0.689343
2017-01-04 0.400337
Freq: D, dtype: float64
----------------------------------------------------------------------------------------------------
ts.shift(2, freq='D') =
2017-01-03 0.038605
2017-01-04 0.074905
2017-01-05 0.689343
2017-01-06 0.400337
Freq: D, dtype: float64
--------------------------------------------------
ts.shift(2, freq='T') =
2017-01-01 00:02:00 0.038605
2017-01-02 00:02:00 0.074905
2017-01-03 00:02:00 0.689343
2017-01-04 00:02:00 0.400337
Freq: D, dtype: float64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Process finished with exit code 0