数据类型及操作
Python 标准库的datetime
datetime模块中的datetime,time,calendar等类都可以用来存储时间类型以及一些其他转换与运算
for example:
from datetime import datetime
now=datetime.now()
print now
#2016-09-11 16:02:35.788000
delta=datetime(2010,2,2)-datetime(2010,2,1)
#2016-09-12 16:04:01.845000
ddatetime的对象见的减法运算会得到一个timedelta对象,表示一个时间段
datetime 对象与它所保存的字符格式时间戳之间可以相互转化,str()函数可用,更推荐datetime.strptime()。这2两个方法之间可以相互转换
str(now)
now.strftime('%Y-%m-%d')
print now.strftime('%Y-%m-%d %H:%M:%S %Z')
strptime and strptime ---------------------------
datetiem.strptime('2001-10-1','%Y-%m-%d') # can not be strftime
将格式为datetime 转换为 TimeStamp
pd.to_datetime(now)

本文介绍了Python中使用datetime模块处理日期数据的方法,包括创建、转换和运算。然后,讨论了Pandas中的时间序列,如设置日期索引、调整频率、日期范围生成以及移动数据。此外,还展示了如何在Pandas DataFrame中处理日期数据,如pivot_table操作和数据拼接。
最低0.47元/天 解锁文章
745

被折叠的 条评论
为什么被折叠?



