在寫爬蟲程式時,常會需要利用日期序列,來抓每一日的網路資料。
這時候,知道如何產生時間序列就很重要!
1. datetime:
import datetime begin = datetime.datetime(2019, 1, 1) end = datetime.datetime(2019, 1, 3) step = datetime.timedelta(days=1) DatePeriod = [] while begin <= end: DatePeriod.append(begin.strftime('%Y%m%d')) begin += step print(DatePeriod) print(type(DatePeriod)) for Day in DatePeriod: print(Day) print(type(Day))
2. pandas:
import pandas as pd import numpy as np # 設定起訖日期DatePeriod = pd.date_range("2019/1/1", "2019/1/3", freq='D') # 將DatePeriod格式化成像要的格式,以及最重要的把時間由index改為string
dp1 = DatePeriod.strftime('%Y%m%d') print(DatePeriod) print(type(dp1)) for Day in dp1: print(Day) print(type(Day))
PS:
strftime這函式很重要,可以把時間序列從index變為string!
date = dataframe.index #date is the datetime index
date = dates.strftime('%Y-%m-%d') #this will return you a numpy array, element is string.
dstr = date.tolist() #this will make you numpy array into a list
PPS:
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 |
參考來源:
Python Generate Datetime Series
python+pandas+時間、日期以及時間序列處理方法
https://stackoverflow.com/questions/30132282/datetime-to-string-with-series-in-python-pandas
------------------------------------------------------------------------------------------------------
http://www.coco-in.net/forum.php?mod=viewthread&tid=38351&page=2
也可以參考別人寫的
------------------------------------------------------------------------------------------------------
http://www.coco-in.net/forum.php?mod=viewthread&tid=38351&page=2
也可以參考別人寫的
以下說明日期字串的產生方式與迴圈 以下是粗略的代碼,請自行調整成你所要的 假設先指定開始與結束日期字串: 我習慣用西元,在後面會轉成民國年 轉換成日期型別: 開始日期與結束日期間的迴圈: |
沒有留言:
張貼留言