在 Excel 很簡單的拖曳就可以產生連續的日期們,而在 Python 你可以利用 datetime 和 timedelta 兩個模組做到類似的事情,不管是要間隔一小時、間格一天、間隔一個月都可以做到。
其實蠻簡單的,只是有時腦袋卡住沒想過可以這樣作,用 timedelta 搭配迴圈就可以產生一系列的時間字串並存到陣列,以下範例是每 5 秒間隔的時間陣列:
import datetime
dt = datetime.datetime(2010, 12, 1,)
end = datetime.datetime(2010, 12, 30)
step = datetime.timedelta(days=1)
result = []
while dt < end:
  result.append(dt.strftime('%Y-%m-%d %H:%M:%S'))
  dt += step
print(result)
當然也可以用 List Comprehension 的方式改寫來加快速度,或是視時機用 Generator 來處理。
import datetime as dt print([dt.date(2017,1,1)+dt.timedelta(days=i) for i in range(30)])
不過真要說最方便的,莫過於 Pandas 提供的 date_range 函式,替你將產生時間序列的產生器寫成函式,效能好又節省空間,不像用迴圈產生時如果範圍很大的話會需要等上一段時間,讓我們來看看範例吧:
# 從 2017/1/1 開始產生以天為單位產生 100 個項目
import pandas as pd
result = pd.date_range('2017/1/1 10:00:00', periods=100, freq='D')
print(result.tolist())
print(result[0:5].to_pydatetime())
for dt in result:
  print(dt.to_pydatetime())
freq 的部分除了 D 也可以換成其他參數,例如 H 是小時、M 是月份,或參考下表:
| Alias | Description | 
|---|---|
| B | business day frequency | 
| C | custom business day frequency (experimental) | 
| 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 endfrequency | 
| QS | quarter start frequency | 
| BQS | business quarter start frequency | 
| A | year end frequency | 
| BA | business year end frequency | 
| AS | year start frequency | 
| BAS | 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 |