借助于pandas库,本文主要介绍时间索引如何生成。
一、生成时间索引
(1)使用pd.to_datetime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import pandas as pd import copy
def ts_builder(data, date_col, new_name=None): data = copy.deepcopy(data) if new_name == None: data.index = pd.to_datetime(data[date_col]) data.drop(date_col, axis=1, inplace=True) else: data[new_name] = pd.to_datetime(data[date_col]) data.index = data[new_name] data.drop([date_col, new_name], axis=1, inplace=True) return data
df = pd.read_csv('daily-min-temperatures.csv') print(df.head())
df2 = ts_builder(df, 'Date') print(df2.head())
df3 = ts_builder(df, 'Date', 'ds') print(df3.head())
|
(2)使用dateparse
1 2 3 4 5 6 7 8 9
| import pandas as pd
df = pd.read_csv('daily-min-temperatures.csv', parse_dates=['Date']) print(df.head()) print(df.dtypes)
df2 = pd.read_csv('daily-min-temperatures.csv', parse_dates=['Date'], index_col='Date') print(df2.head()) print(df2.dtypes)
|
二、时间索引使用技巧
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import pandas as pd
ts = pd.read_csv('daily-min-temperatures.csv', parse_dates=['Date'], index_col='Date')
print(ts.loc['1981-01-02':'1981-01-06']) print(ts.loc[:'1981-01-06']) print(ts.loc['1981-01'])
from datetime import datetime print(ts.iloc[0].values) print(ts.loc['1981-01-01'].values) print(ts.loc[datetime(1981,1,1)].values)
|
数据集下载
链接:https://pan.baidu.com/s/1y81GD_zbDxbGvaE0MqRsNQ
提取码:3dep