所谓的时刻数据代表时间点,是pandas的数据类型,是将值与时间点相关联的最基本类型的时间序列数据。
1.Timestamp
Timestamp
是将数据类型转化为pandas的Timestamp类型
import pandas as pd import datetime date1 = datetime.datetime(2019, 12, 31, 12, 1, 2) # 创建一个datetime.datetime date2 = '2017-12-31' # 创建一个字符串 t1 = pd.Timestamp(date1) t2 = pd.Timestamp(date2) print(t1,type(t1)) print(t2,type(t2))
2.to_datetime
to_datetime
也是将数据类转化为pandas的Timestamp类型,但是如果是多个时间,则会转化为pandas的DatetimeIndex
2.1 单个时间转化
import pandas as pd from datetime import datetime date1 = datetime(2019, 12, 31, 12, 1, 2) # 创建一个datetime.datetime date2 = '2017-12-31' # 创建一个字符串 # pd.to_datetime():如果是单个时间数据,转换成pandas的时刻数据,数据类型为Timestamp t1 = pd.to_datetime(date1) t2 = pd.to_datetime(date2) print(t1,type(t1)) print(t2,type(t2))
2.2 多个时间转化
import pandas as pd from datetime import datetime # 多个时间数据,将会转换为pandas的DatetimeIndex list_date = ['2019-12-31','2020-01-01','2020-01-02'] t3 = pd.to_datetime(list_date) print(t3,type(t3)) print('-' * 50) # 分割线 date1 = [datetime(2018,6,1),datetime(2018,7,1),datetime(2018,8,1),datetime(2018,9,1),datetime(2018,10,1)] date2 = ['2019-2-1','2019-2-2','2019-2-3','2019-2-4','2019-2-5','2019-2-6'] print(date1) print(date2) print('-' * 50) # 分割线 t1 = pd.to_datetime(date1) t2 = pd.to_datetime(date2) print(t1) print(t2) print('-' * 50) # 分割线 date3 = ['2017-2-1','2017-2-2','2017-2-3','hello world!','2017-2-5','2017-2-6'] t3 = pd.to_datetime(date3, errors = 'ignore') print(t3,type(t3)) # 当一组时间序列中夹杂其他格式数据,可用errors参数返回 # errors = 'ignore':不可解析时返回原始输入,这里就是直接生成一般数组 t4 = pd.to_datetime(date3, errors = 'coerce') print(t4,type(t4)) # errors = 'coerce':不可扩展,缺失值返回NaT(Not a Time),结果认为DatetimeIndex
来源:https://www.cnblogs.com/OliverQin/p/12283511.html