内容承接上一篇文章,本文章主要介绍时间序列数据的相关Pandas知识。
一、时间模块
时间模块指的是datetime,主要需要学习datetime.date(),datetime.datetime(),datetime.timedelta()。
import
now
# datetime.timedelta时间差
t1=datetime.datetime(2000,10,1)
tx=datetime.timedelta(100)
print(t1+tx)
print(t1-tx)
# 字符串转换方法 parser.parse
from dateutil.parser import parse
# from numpy import random
date='12-21-2017'
date2='21/12/2017'
print(parse(date),type(parse(date)))
print(parse(date2))
二、时刻数据
1、Pandas时刻数据:Timestamp
时刻数据代表时间点,是pandas的数据类型,是将值与时间点相关联的最基本类型的时间序列数据。
import numpy as np
import pandas as pd
import datetime
date1='20200101'
date2=datetime.datetime(2020,1,1,15,0)
t1=pd.Timestamp(date1)
t2=pd.Timestamp(date2)
print(t1,type(t1))
print(t2,type(t2))
print(date2,type(date2))
# 直接生成pandas的时刻数据→时间戳
# 数据类型为pandas的Timestamp