python的时间操作,内置模块有time、datetime、 calendar 。datetime模块,是对time的封装,提供了更丰富的接口
1.内置time模块
time模块中有三种格式: timestamp时间戳 struct_time时间元组 format time 格式化时间 timestamp 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 struct_time时间元组 共9个元素组成。用一个元组装起来的9组数字。可以通过索引访问相应的值,为年、月、日、时、分、秒、一周第几日、一年第几日、夏令时 format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式
time模块的函数方法
time.time() #查看当前时间戳
time.sleep()
线程将推迟指定的时间后运行下面的代码,单位为秒。其精度为亚秒级
time.localtime(seconds=None)
将一个时间戳转化为当前地区的一个struct_time()类型元组,默认当前时间
time.mktime(p_tuple)
将一个 struct_time()类型元组 转换为时间戳
time.ctime(seconds=None)
将一个时间戳换成一个时间字符串 ,默认当前时间
time.strptime()
将 struct_time() 转换为字符串
time.strftime()
将字符串格式的日期转换为 time.strptime()
print(time.time()) #当前时间戳
print(time.localtime()) #将时间戳转换为struct_time
print(time.mktime((2020,4,24,5,2,3,4,115,0))) #将struct_time转换为时间戳
print(time.ctime()) #将时间戳转换为字符串
print(time.strftime('%Y-%m-%d',time.localtime())) #将struct_time转换为字符串
print(time.strptime('2020/04/05','%Y/%m/%d')) #将字符串转换为struct_time
2. 内置datetime模板,有date、time、datetime、timedelta四种数据类型
date 存储年月日 time 存储时分秒 datetime 以毫秒形式存储日期和时间,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 timedelta 两个以datetime数据格式之间的时间差
date类
date对象由year年份、month月份及day日期三部分构成:datetime.date(year, month, day)
from datetime import datetime,date,timedelta,time a = date.today() print(a) print(a.year,a.month,a.day) 2020-04-24 2020 4 24
date datetime calendar
datetime 数据类型==> date time datetime timedelta
datetime.now() ==>datetime.datetime(year, m, d, h, m, s,ms) ==>now.year ==>datatime以毫秒存储日期时间
1.datetime与str
利用str或strftime方法
str(date(year,month,day))
datetime(year,m,day).strftime(‘%Y-%m-%d’)
datatime.strptime方法将str转换为日期,通过已知的格式进行日期解析
datestrs=[‘2016/6/1′,’2016/8/6′] ==> [datatime.strptiem(x,’%Y/%m/%d’) for x in datestrs]
from dateutil.parser import parse parse('2018/02/03 02:03:02') parse可以自动解析通用的时间日期格式类,将字符串转换为datetime对象,减少用datetime.strptime()每次指定格式
pandas时间处理
pandas时间序列中,相应也存在时间戳、时间日期的序列
pandas时间戳序列: pd.date_range(”,periods=),返回DtatimeIndex的时间序列对象
dt = pd.date_range('20200301',periods=10) print(dt) DatetimeIndex(['2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05', '2020-03-06', '2020-03-07', '2020-03-08', '2020-03-09', '2020-03-10'], dtype='datetime64[ns]', freq='D')
pandas时期对象:pd.Period()
dt = pd.Period('2010',freq='M') print(dt) 2010-01
pandas的时期序列:pd.period_range()
dt = pd.period_range('2010-01-01',freq='M',periods=10) print(dt) PeriodIndex(['2010-01', '2010-02', '2010-03', '2010-04', '2010-05', '2010-06','2010-07', '2010-08', '2010-09', '2010-10'],dtype='period[M]', freq='M') 返回的是PeriodIndex,时期序列对象
pandas时间序列
时间戳的时间序列和时期的时间序列相互转换
dt = pd.DataFrame(np.arange(10),index=pd.date_range('20200101',periods=10)) print(dt.index) DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10'], dtype='datetime64[ns]', freq='D') newdt = dt.to_period() print(newdt.index) PeriodIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10'], dtype='period[D]', freq='D') DateFrame的索引为DatetimeIndex对象,可以用to_period()方法,将时间戳序列转换为时间日期序列,默认时间间隔为天 olddt = newdt.to_timestamp() print(olddt.index) DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10'], dtype='datetime64[ns]', freq='D') 同样,时间日期序列,可以使用to_timestamp()方法转换为时间戳序列
时间序列和时间戳直接相互转换时,会产生时间精度丢失
pandas时间序列重采样
resample()
dt = pd.DataFrame(np.random.rand(10),index=pd.date_range('2020-02-01 09:50',periods=10,freq='T')) print(dt) 2020-02-01 09:50:00 0.830833 2020-02-01 09:51:00 0.419032 2020-02-01 09:52:00 0.118628 2020-02-01 09:53:00 0.281052 2020-02-01 09:54:00 0.716910 2020-02-01 09:55:00 0.611131 2020-02-01 09:56:00 0.599084 2020-02-01 09:57:00 0.948396 2020-02-01 09:58:00 0.510810 2020-02-01 09:59:00 0.765798 new = dt.resample('2min').sum() print(new) 2020-02-01 09:50:00 1.740691 2020-02-01 09:52:00 1.497430 2020-02-01 09:54:00 0.444975 2020-02-01 09:56:00 0.503731 2020-02-01 09:58:00 1.402243 resample时间戳重采样,设置采样间隔和聚合方式 groupby的两种方式: newdt = dt.groupby(lambda x:datetime.datetime.strftime(x,'%Y-%m-%d')).sum() newdt = dt.groupby(dt.index.to_period('D')).sum() print(newdt) 2020-02-01 5.521197 两种效果一样,一种是直接转换成字符串,一种是将时间戳转换成时间日期序列,区别在于第一种是字符串对象,第二种的index是时间序列对象