日期和时间数据类型及工具
获取当前时间:
from datetime import datetime
now=datetime.now()
print(now)
运行结果:
如果想获取当前的年月日,只需要变成now.year, now.month和now.day
字符串和datetime相互转换
先上代码:
stamp=datetime(2011,1,3)
print(str(stamp))
print(stamp.strftime('%y-%m-%d'))
其中,运行结果为:
str(一个时间序列)可以将一个时间序列转换成字符串
如果想要解析时间,则需要用到parse()
from dateutil.parser import parse
print(parse('2011-1-3'))
运行结果:
如果平时的时日在前面,则需要用到
print(parse('23/1/2033',dayfirst=True))
时间序列基础
dates = [datetime(2011, 1, 2), datetime(2011, 1, 5),datetime(2011, 1, 7), datetime(2011, 1, 8),
datetime(2011, 1, 10), datetime(2011, 1, 12)]
ts=pd.Series(np.random.randn(6),index=dates)
print(ts)
print('-----------------')
print(ts+ts[::2])
其中有几个知识点:
一个是时间序列也可以作为索引添加到Series里面,第二个是,时间序列相加的时候,会自动按照时间对齐(如果有一个没有该数据,则会变成nall)
索引、选取、子集构造
stamp2=ts.index[2]
print(ts[stamp2])
print(ts['1/7/2011'])
在这里面,我们可以向该Series里面传入任何可以被解释为时间的字符串
也可以传入不完整的时间,例如
print(ts['2011/1'])
运行结果:
以下的方式也是正确的,即得到1.6日之后的数据
print(ts['2011/1/6':])
print(ts[:'2011/1/7'])
运行结果分别是6日之后的和7日之前的(包含6日和7日)
日期的范围、频率以及移动
生成日期范围
index = pd.date_range('2012-04-01', '2012-06-01')
index2=pd.date_range('2012-04-01',periods=20)
index3=pd.date_range(end='2012-04-01',periods=20)
index4=pd.date_range('2012-04-01','2013-02-01',freq='BM')
index5=pd.date_range('2012-04-01','2013-02-01',freq='4h30min')
#print(index)
#print(index2)
其中,freq=‘BM’代表的是只包含每月的月末一天,其余的时间序列基础频率还包括:
移动数据
单纯移动:
print(ts.shift(2))
只传给shift一个数字,可以实现单纯的数据移动,运行结果见下图,即数据从上到下移动
如果想实现日期的移动,则可以传给shift一个freq,即
print(ts.shift(2,freq='d'))
即实现了日期的移动
时区处理
本地化和转换
先上代码
import pytz
tz=pytz.timezone('US/Eastern')
print(tz)
print(ts.index.tz)
temp2=pd.date_range('2020/1/2',periods=5,freq='d',tz='UTC')
ts2=pd.Series(np.random.randn(5),index=temp2)
print(ts2.index.tz)
其中,tz代表时区,输出结果为:
从某个时区到本地时区的转换用到了
ts2=ts2.tz_localize('UTC')
转换到其他时区用到了
ts2=ts2.tz_convert('UTC')
时期及算术运算
时期创建方式:
p=pd.Period(2007)
#print(p+5)
时期和日期可以互相转换:
print(p.to_timestamp())
得到结果:
转换为时期为:
t=pd.Timestamp('2007.7.4',freq='m')
print(t.to_period())
注意一点:转换为时期的日期,必须有freq,不然 无法转换
重采样及频率转换
运用resample方法
rng=pd.date_range('2020/1/14',periods=60,freq='d')
ts3=pd.Series(np.random.randn(60),index=rng)
print(ts3.resample('m').mean())
其中还可以传入参数:label='left',closed='left',其意义是:标签取左侧,左侧区间封闭
完整代码:
from datetime import datetime
import pandas as pd
import numpy as np
from dateutil.parser import parse
import pytz
now=datetime.now()
#print(now)
delat=datetime(2022,7,1)-datetime.now()
#print(delat)
#print(delat.seconds)
#print(delat.days)
start=datetime(2011,1,7)
stamp=datetime(2011,1,3)
#print(str(stamp))
#print(stamp.strftime('%y-%m-%d'))
#print(parse('23/1/2033',dayfirst=True))
dates = [datetime(2011, 1, 2), datetime(2011, 1, 5),datetime(2011, 1, 7), datetime(2011, 1, 8),
datetime(2011, 1, 10), datetime(2011, 1, 12),]
ts=pd.Series(np.random.randn(6),index=dates,)
#print(ts)
#('-----------------')
#print(ts+ts[::2])
stamp2=ts.index[2]
#print(ts[stamp2])
#print(ts['1/7/2011'])
#print(ts[:'2011/1/7'])
#print(ts.resample('d'))
index = pd.date_range('2012-04-01', '2012-06-01')
index2=pd.date_range('2012-04-01',periods=20)
index3=pd.date_range(end='2012-04-01',periods=20)
index4=pd.date_range('2012-04-01','2013-02-01',freq='4h30min')
index5=pd.date_range('2012-04-01','2013-02-01',freq='4h30min')
#print(index4)
#print(index)
#print(index2)
#print(ts.shift(2))
#print(ts.shift(2,freq='d'))
tz=pytz.timezone('US/Eastern')
#print(tz)
#print(ts.index.tz)
temp2=pd.date_range('2020/1/2',periods=5,freq='d',tz='UTC')
ts2=pd.Series(np.random.randn(5),index=temp2)
#print(ts2.index.tz)
p=pd.Period(2007)
#print(p+5)
#print(p.to_timestamp())
t=pd.Timestamp('2007.7.4',freq='m')
#print(t.to_period())
rng=pd.date_range('2020/1/14',periods=60,freq='d')
ts3=pd.Series(np.random.randn(60),index=rng)
print(ts3.resample('m',label='left',closed='left').mean())
ts3=ts3.shift(1,'m')
print(ts3.resample('m',label='left',closed='left').mean())