基础 | date_range时间序列--时间切片

文章目录

  • 1.1 工作日 (freq='b')
  • 1.2 每2天(freq='2d')
  • 1.3 生成10个长度的时间(periods=10)
  • 1.4 按月生成10个长度的时间(periods=10, freq='M')
  • 1.5 左闭右开(closed='left')
  • 实例1:将一天24小时按5分钟频率生成时间片区间
  • 实例2:给定一个时间,输出该时间所属时间片编号,即时间片区间索引

大家好,我是 【Python当打之年】

今天简单介绍date_range freq 几个参数实例

函数语法:

date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)

几个主要参数:

参数 类型 说明
start str 开始日期
end str 结束日期
periods int 时间数量
freq str 时间频率
closed str 闭合区间, 取值:{None, ‘left’, ‘right’}

注意:start、end、periods三个参数必须取两个

freq 一般取值:

取值 说明
M
B 工作日
W 星期
D
H 小时
T 分钟
S
L 毫秒

1.1 工作日 (freq=‘b’)

pd.date_range(start='20220101', end='20220131',  freq='b')
# 输出
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',
               '2022-01-19', '2022-01-20', '2022-01-21', '2022-01-24',
               '2022-01-25', '2022-01-26', '2022-01-27', '2022-01-28',
               '2022-01-31'],
              dtype='datetime64[ns]', freq='B')

1.2 每2天(freq=‘2d’)

pd.date_range(start='20220101', end='20220131', freq='2d')
# 输出
DatetimeIndex(['2022-01-01', '2022-01-03', '2022-01-05', '2022-01-07',
               '2022-01-09', '2022-01-11', '2022-01-13', '2022-01-15',
               '2022-01-17', '2022-01-19', '2022-01-21', '2022-01-23',
               '2022-01-25', '2022-01-27', '2022-01-29', '2022-01-31'],
              dtype='datetime64[ns]', freq='2D')

1.3 生成10个长度的时间(periods=10)

pd.date_range(start='20220101', periods=10,  freq='d')
# 输出
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'],
              dtype='datetime64[ns]', freq='D')

1.4 按月生成10个长度的时间(periods=10, freq=‘M’)

pd.date_range(start='20220101', periods=10,  freq='M')
# 输出
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31',
               '2022-09-30', '2022-10-31'],
              dtype='datetime64[ns]', freq='M')

1.5 左闭右开(closed=‘left’)

pd.date_range(start='20220101', end='20220131',  freq='d', closed='left')
# 输出
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',
               '2022-01-13', '2022-01-14', '2022-01-15', '2022-01-16',
               '2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20',
               '2022-01-21', '2022-01-22', '2022-01-23', '2022-01-24',
               '2022-01-25', '2022-01-26', '2022-01-27', '2022-01-28',
               '2022-01-29', '2022-01-30'],
              dtype='datetime64[ns]', freq='D')

其他参数可以自己试一下

实例1:将一天24小时按5分钟频率生成时间片区间

def get_time_ranges(from_time, to_time, frequency):
    from_time, to_time, frequency = pd.to_datetime(from_time), pd.to_datetime(to_time), frequency*60
    time_range = list(pd.date_range(from_time, to_time, freq=f'{frequency}s'))
    if to_time not in time_range:
        time_range.append(to_time)
    time_range = [item.strftime('%Y-%m-%d %H:%M:%S') for item in time_range]
    time_ranges = []
    for item in time_range:
        f_time = item
        t_time = (datetime.datetime.strptime(item, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(seconds=frequency))
        if t_time >= to_time:
            t_time = to_time.strftime('%Y-%m-%d %H:%M:%S')
            time_ranges.append([f_time, t_time])
            break
        time_ranges.append([f_time, t_time.strftime('%Y-%m-%d %H:%M:%S')])
    return time_ranges

from_time = '2022-09-15 00:00:00'
to_time = '2022-09-15 23:59:59'   
frequency = 5 
get_time_ranges(from_time, to_time, frequency)

# 输出 一共288个区间
[['2022-09-15 00:00:00', '2022-09-15 00:05:00'],
 ['2022-09-15 00:05:00', '2022-09-15 00:10:00'],
 ['2022-09-15 00:10:00', '2022-09-15 00:15:00'],
 ['2022-09-15 00:15:00', '2022-09-15 00:20:00'],
 ['2022-09-15 00:20:00', '2022-09-15 00:25:00'],
 ['2022-09-15 00:25:00', '2022-09-15 00:30:00'],
 ...
 ['2022-09-15 23:40:00', '2022-09-15 23:45:00'],
 ['2022-09-15 23:45:00', '2022-09-15 23:50:00'],
 ['2022-09-15 23:50:00', '2022-09-15 23:55:00'],
 ['2022-09-15 23:55:00', '2022-09-15 23:59:59']]

实例2:给定一个时间,输出该时间所属时间片编号,即时间片区间索引

def time_num(timeStamp):
    timeStamp0 = timeStamp.split(' ')[0]
    from_time = f'{timeStamp0} 00:00:00'
    to_time = f'{timeStamp0} 23:59:59'    
    frequency = 5
    time_ranges = get_time_ranges(from_time, to_time, frequency)
    for idx,value in enumerate(time_ranges):
        if value[0] < timeStamp <= value[1]:
            return idx
    return 0

time_num('2019-09-15 23:53:00')
# 输出
286

time_num('2022-09-10 10:53:00')
# 输出
130

以上就是本期为大家整理的全部内容了,赶快练习起来吧,如有疑问可后台联系小编,喜欢的朋友可以点赞、收藏也可以分享注明出处)让更多人知道。

你可能感兴趣的:(Python基础,python)