假設目前已經引入了 pandas,同時也擁有 pandas 的 DataFrame 類型數據。
import pandas as pd
數據集如下
df.head(3)
date open close high low volume code
0 2006-12-18 3.905 3.886 3.943 3.867 171180.67 600001
1 2006-12-19 3.886 3.924 3.981 3.867 276799.39 600001
2 2006-12-20 3.934 3.934 3.962 3.809 265653.85 600001
查看每一列的類型
df.info()
從結果的第四排可以看見 date 這一列類型是"object",即字符類型。
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 640 entries, 0 to 639
Data columns (total 7 columns):
date 640 non-null object
open 640 non-null float64
close 640 non-null float64
high 640 non-null float64
low 640 non-null float64
volume 640 non-null float64
code 640 non-null object
dtypes: float64(5), object(2)
memory usage: 35.1+ KB
現在的目標是:
- 把 date 這一列用作索引
- 把 date 用作索引時,類型需要是 DatetimeIndex。
方法1: .to_datetime 和 .set_index
首先,利用 pandas 的to_datetime
方法,把 "date" 列的字符類型數據解析成 datetime 對象。
然后,把 "date" 列用作索引。
df['date'] = pd.to_datetime(df['date'])
df.set_index("date", inplace=True)
結果:
df.head(3)
open close high low volume code
date
2006-12-18 3.905 3.886 3.943 3.867 171180.67 600001
2006-12-19 3.886 3.924 3.981 3.867 276799.39 600001
2006-12-20 3.934 3.934 3.962 3.809 265653.85 600001
查看索引是否成為 DatetimeIndex 類型,可以看見確實已經成功轉化類型。
df.axes
[DatetimeIndex(['2006-12-18', '2006-12-19', '2006-12-20', '2006-12-21',
'2006-12-22', '2006-12-25', '2006-12-26', '2006-12-27',
'2006-12-28', '2006-12-29',
...
'2009-12-02', '2009-12-03', '2009-12-04', '2009-12-07',
'2009-12-08', '2009-12-09', '2009-12-10', '2009-12-11',
'2009-12-14', '2009-12-15'],
dtype='datetime64[ns]', name='date', length=640, freq=None),
Index(['open', 'close', 'high', 'low', 'volume', 'code'], dtype='object')]
方法2: .DatetimeIndex
首先是原始數據。
df2.head(3)
date open close high low volume code
0 2003-08-01 4.997 4.949 5.016 4.949 20709.15 600002
1 2003-08-04 4.949 5.045 5.054 4.949 23923.35 600002
2 2003-08-05 5.054 5.093 5.131 5.006 35224.00 600002
先把 "date" 列用作索引,然后使用 DatetimeIndex
將字符類型轉化成 DateIndex
。
df2.set_index("date", inplace=True)
這個時候索引還是 object 類型,就是字符串類型。
df2.axes
[Index(['2003-08-01', '2003-08-04', '2003-08-05', '2003-08-06', '2003-08-07',
'2003-08-08', '2003-08-11', '2003-08-12', '2003-08-13', '2003-08-14',
...
'2006-03-24', '2006-03-27', '2006-03-28', '2006-03-29', '2006-03-30',
'2006-03-31', '2006-04-03', '2006-04-04', '2006-04-05', '2006-04-06'],
dtype='object', name='date', length=640),
Index(['open', 'close', 'high', 'low', 'volume', 'code'], dtype='object')]
將其轉化成 DateIndex 類型。
df2.index = pd.DatetimeIndex(df.index)
再次查看結果
df2.axes
轉化成功
[DatetimeIndex(['2006-12-18', '2006-12-19', '2006-12-20', '2006-12-21',
'2006-12-22', '2006-12-25', '2006-12-26', '2006-12-27',
'2006-12-28', '2006-12-29',
...
'2009-12-02', '2009-12-03', '2009-12-04', '2009-12-07',
'2009-12-08', '2009-12-09', '2009-12-10', '2009-12-11',
'2009-12-14', '2009-12-15'],
dtype='datetime64[ns]', name='date', length=640, freq=None),
Index(['open', 'close', 'high', 'low', 'volume', 'code'], dtype='object')]
結論:.to_datetime
僅轉換格式,.DatetimeIndex
還能設置為索引
兩者在轉化格式的功能上效果一樣,都可以把字符串對象轉換成 datetime 對象。
pd.DatetimeIndex 是把某一列進行轉換,同時把該列的數據設置為索引 index。
比如
df2.index = pd.DatetimeIndex(df2["date"])
得到一個以 date 作為索引的結果。
.DatetimeIndex
的問題是原來的 date 列數據仍然存在,形成了重復。
date open close high low volume code
date
2003-08-01 2003-08-01 4.997 4.949 5.016 4.949 20709.15 600002
2003-08-04 2003-08-04 4.949 5.045 5.054 4.949 23923.35 600002
2003-08-05 2003-08-05 5.054 5.093 5.131 5.006 35224.00 600002
最終還需要把 date 這一列刪掉。
del df2["date"]
才能得到正常數據
open close high low volume code
date
2003-08-01 4.997 4.949 5.016 4.949 20709.15 600002
2003-08-04 4.949 5.045 5.054 4.949 23923.35 600002
2003-08-05 5.054 5.093 5.131 5.006 35224.00 600002