日期时间变量的处理
一、时间戳Timestame对象
二、获取当前时间
三、时间戳实例的属性和方法
四、计算时差Timedelta实例
五、生成时间戳范围
六、数据框内对时间变量的操作
七、滞后一期和先导一期
八、日期时间变量做数据框的索引
一、时间戳Timestame对象
import pandas as pd
import numpy as np
import time
import datetime
pd.Timestamp('2017-01-02T12')
pd.Timestamp(1,unit='s')
pd.Timestamp(2017,1,1)
pd.Timestamp(pd.datetime(2014,1,1))
pd.to_datetime('2019/08/28',format='%Y/%m/%d',errors='coerce')
pd.to_datetime(1,unit='s')
pd.to_datetime(pd.datetime(2019,8,28))
二、获取当前时间
pd.datetime.now()
datetime.datetime.now()
time.localtime()
datetime.datetime.now().timetuple()
三、时间戳实例的属性和方法
a=pd.Timestamp("1970/1/1T00:01:20")
a.year
a.month
a.day
a.hour
a.minute
a.second
a.dayofweek
a.date()
a.time()
a.timestamp()
a.value
a.strftime("%m/%d/%Y")
a.strftime("%A")
四、计算时差Timedelta实例
a=pd.Timestamp("2019/8/28T13:48:00")
b=pd.Timestamp("2019/8/28T9:48:00")
d=a-b
Out[36]:
Timedelta('0 days 04:00:00')
cd=pd.Timedelta(days=1,hours=2,minutes=3,seconds=4,
milliseconds=5,microseconds=6,nanoseconds=7)
Out[39]:
Timedelta('1 days 02:03:04.005006')
cd.total_seconds()
cd.delta
cd.components
cd.components.days
cd.components.seconds
cd.components.milliseconds
cd.components.microseconds
cd.components.nanoseconds
五、生成时间戳范围
a=pd.date_range('2001-1-1',freq='D',periods=10)
Out[47]:
DatetimeIndex(['2001-01-01', '2001-01-02', '2001-01-03', '2001-01-04',
'2001-01-05', '2001-01-06', '2001-01-07', '2001-01-08',
'2001-01-09', '2001-01-10'],
dtype='datetime64[ns]', freq='D')
a[0]
六、数据框内对时间变量的操作
data1=pd.read_excel('D:\CDA数分学习资料\python\数据清洗\pandas\\上证指数日收盘价数据.xls')
data1.head()
data1.info()
data1.drop(columns=['index'],inplace=True)
data1['num']=data1['date'].map(lambda x:x.timestamp())
data1['ym']=data1['date'].dt.to_period(freq='m')
data1['date1']=data1.apply(lambda x:pd.Timestamp(x['year'],x['month'],x['day']),axis=1)
data1["datestr"].map(lambda x:pd.Timestamp(x))
pd.to_datetime(data1["datestr"],
format="%m/%d/%Y",
errors="ignore").head()
data1["datestr"].map(lambda x:pd.to_datetime(x,format="%Y/%m/%d"))
data1['datestr']=data1['date'].map(lambda x:x.strftime('%Y/%m/%d'))
data1['datestr1']=data1['date'].dt.strftime('Y%/%m/%d')
data1["date"].dt.year
data1["date"].dt.month
data1["date"].dt.day
data1["date"].dt.date
data1["date"].dt.time
data1["year"]=data1["date"].map(lambda x:x.year)
data1["date"].map(lambda x:x.time())
data1['date2']=pd.to_datetime(data1["num"],unit="s")
data1["num"].map(lambda x:pd.to_datetime(x,unit='s'))
data1["num"].map(lambda x:pd.Timestamp(x,unit='s'))
data1['date']+pd.Timedelta(days=30)
七、滞后一期和先导一期
data1["p"].shift(1)
data1["p"].shift(-1).tail()
data2['r1']=data2['p'].map(math.log).diff()
八、日期时间变量做数据框的索引
data2.index=data2['date']
data1["p"]["2003-1-06"]
data1["p"]["2003-2"]
data1["p"]['2003-1-06':'2003-1-10']
data1[(data1.index>"2003-1-6") & (data1.index<"2003-1-15")]
series1=pd.date_range("2003-1-1",freq="D",periods=100)
data1=pd.DataFrame(np.random.rand(100,2),columns=["x1","x2"])
data1.index=series1
data2=data1.asfreq(freq='5D')
data2.asfreq(freq="D")
data2.asfreq(freq="D",method="pad")