1. 程式人生 > 實用技巧 >pandas生成指定間隔的連續時間列表

pandas生成指定間隔的連續時間列表

pandas生成指定間隔的連續時間列表

import pandas as pd
print(pd.date_range("2020-12-09 10:26:01","2020-12-09 10:26:15",freq='S'))
print(pd.date_range("2020-12-09 10:26:01","2020-12-09 10:26:15",freq='10S'))
print(pd.date_range("2020-12-09 10:22:01","2020-12-09 10:26:02",freq='Min'))
print(pd.date_range("2020-12-09 10:22:01","2020-12-09 15:26:02",freq='H'))
print(pd.date_range("2020-12-09 10:22:01","2020-12-15 10:26:02",freq='D'))
print(pd.date_range("2020-12-09 10:22:01","2020-12-31 10:26:02",freq='W'))
print(pd.date_range("2020-01-09 10:22:01","2020-12-09 10:26:02",freq='M'))
print(pd.date_range("2020-12-09 10:22:01","2030-12-09 10:26:02",freq='Y'))


result:
DatetimeIndex(['2020-12-09 10:26:01', '2020-12-09 10:26:02',
               '2020-12-09 10:26:03', '2020-12-09 10:26:04',
               '2020-12-09 10:26:05', '2020-12-09 10:26:06',
               '2020-12-09 10:26:07', '2020-12-09 10:26:08',
               '2020-12-09 10:26:09', '2020-12-09 10:26:10',
               '2020-12-09 10:26:11', '2020-12-09 10:26:12',
               '2020-12-09 10:26:13', '2020-12-09 10:26:14',
               '2020-12-09 10:26:15'],
              dtype='datetime64[ns]', freq='S')
DatetimeIndex(['2020-12-09 10:22:01', '2020-12-09 10:23:01',
               '2020-12-09 10:24:01', '2020-12-09 10:25:01',
               '2020-12-09 10:26:01'],
              dtype='datetime64[ns]', freq='T')
DatetimeIndex(['2020-12-09 10:22:01', '2020-12-09 11:22:01',
               '2020-12-09 12:22:01', '2020-12-09 13:22:01',
               '2020-12-09 14:22:01', '2020-12-09 15:22:01'],
              dtype='datetime64[ns]', freq='H')
DatetimeIndex(['2020-12-09 10:22:01', '2020-12-10 10:22:01',
               '2020-12-11 10:22:01', '2020-12-12 10:22:01',
               '2020-12-13 10:22:01', '2020-12-14 10:22:01',
               '2020-12-15 10:22:01'],
              dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2020-12-13 10:22:01', '2020-12-20 10:22:01',
               '2020-12-27 10:22:01'],
              dtype='datetime64[ns]', freq='W-SUN')
DatetimeIndex(['2020-01-31 10:22:01', '2020-02-29 10:22:01',
               '2020-03-31 10:22:01', '2020-04-30 10:22:01',
               '2020-05-31 10:22:01', '2020-06-30 10:22:01',
               '2020-07-31 10:22:01', '2020-08-31 10:22:01',
               '2020-09-30 10:22:01', '2020-10-31 10:22:01',
               '2020-11-30 10:22:01'],
              dtype='datetime64[ns]', freq='M')
DatetimeIndex(['2020-12-31 10:22:01', '2021-12-31 10:22:01',
               '2022-12-31 10:22:01', '2023-12-31 10:22:01',
               '2024-12-31 10:22:01', '2025-12-31 10:22:01',
               '2026-12-31 10:22:01', '2027-12-31 10:22:01',
               '2028-12-31 10:22:01', '2029-12-31 10:22:01'],
              dtype='datetime64[ns]', freq='A-DEC')

freq 引數說明

引數 必選 型別 說明
S ture string 秒級
Min ture string
H ture string
D ture string
W ture string
M ture string
Y ture string

注意:級別前面的數字表示多長時間間隔的(5H,就是5小時間隔)