pandas的date_range方法详细说明

start,end,periods,三个参数三选二

In [4]: d = pd.date_range('20200101','20200110')

In [5]: d
Out[5]:
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
               '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
               '2020-01-09', '2020-01-10'],
              dtype='datetime64[ns]', freq='D')

pd.date_range('20200101','20200110')等同于pd.date_range('20200101',periods=10)

以月为周期的时间设定,默认时以月最后一天为周期。如:

In [7]: pd.date_range('20200101',periods=5,freq = 'M')
Out[7]:
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30',
               '2020-05-31'],
              dtype='datetime64[ns]', freq='M')

若需要以一个月的第一天为时间节点,则只需要将上述的freq改为'MS'即可。

In [8]: pd.date_range('20200101',periods=5,freq = 'MS')
Out[8]:
DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01'],
              dtype='datetime64[ns]', freq='MS')

 

参数 数据类型 意义
start str or datetime-like, optional  生成日期的左侧边界
end str or datetime-like, optional 生成日期的右侧边界
periods integer, optional  生成周期
freq str or DateOffset, default ‘D’  可以有多种比如‘5H’,日期间隔,具体见下文
tz str or tzinfo, optional  返回本地化的DatetimeIndex的时区名,例如’Asia/Hong_Kong’
normalize bool, default False 生成日期之前,将开始/结束时间初始化为午夜
name str, default None 产生的DatetimeIndex的名字
closed  {None, ‘left’, ‘right’}, optional 使区间相对于给定频率左闭合、右闭合、双向闭合(默认的None)
**kwargs   为了兼容性,对结果没有影响

Alias    Description
B    business day frequency
C    custom business day frequency
D    calendar day frequency
W    weekly frequency
M    month end frequency
SM    semi-month end frequency (15th and end of month)
BM    business month end frequency
CBM    custom business month end frequency
MS    month start frequency
SMS    semi-month start frequency (1st and 15th)
BMS    business month start frequency
CBMS    custom business month start frequency
Q    quarter end frequency
BQ    business quarter end frequency
QS    quarter start frequency
BQS    business quarter start frequency
A, Y    year end frequency
BA, BY    business year end frequency
AS, YS    year start frequency
BAS, BYS    business year start frequency
BH    business hour frequency
H    hourly frequency
T, min    minutely frequency
S    secondly frequency
L, ms    milliseconds
U, us    microseconds
N    nanoseconds

 

参考:

https://blog.csdn.net/wangqi_qiangku/article/details/79384731
https://blog.csdn.net/The_Time_Runner/article/details/100672635

to be continue......

你可能感兴趣的:(pandas的date_range方法详细说明)