datetime 模块中的数据类型
date 以公历形式存储日历日期(年、月、日)
time 将时间存储为 时 分 秒 毫秒
datetime 存储日期和时间
timedelta 表示两个datetime值之间的差 (日 秒 毫秒)
import pandas
from datetime import datetime
from datetime import timedelta
import numpy
#显示当前时间
#today=datetime.now()
#today1 = datetime.today()
#datatime的使用,用逗号隔开。
print(datetime(2020,8,4,12,12))
sj=pandas.to_datetime('20200820')
#时间差
sjc=datetime(2011,1,7)-datetime(2008,6,21,12,15)
print(sjc.days)
print(sjc.seconds)
print(sjc)
################
start=datetime(2020,1,7)
print(start+timedelta(1))#加一天
print(start-2*timedelta(1))#减2天 (2*1)
#############################
#传入 str 或者strftime datatime对象和pandas的timestamp对象可以被转换为字符串
stamp=datetime(2011,1,3)
print(str(stamp)+"\n"+stamp.strftime('%Y-%m-%d'))
#############使用datetime.strptime 也可以将字符串转换为日期。
value="2019-12-24"
print(datetime.strptime(value,'%Y-%m-%d'))
###################################
#上面的datetime.strptime 每次都要编写格式。
#我们使用dateutil这个第三方包中的parser.parse方法
from dateutil.parser import parse
print(parse("1999.5.17"))
#如果你传入的是国际日期:日在前面月在后面,传入dayfirst=1
print(parse('17/5/1999',dayfirst=1))
to_datetime
to_datetime 方法可以解析多种不同的日期表示形式。
from datetime import datetime
import pandas
datestrs=['7/6/2011','8/9/2011']
print(pandas.to_datetime(datestrs))
# to_datetime还能被用做处理缺失值//他是被处理缺失值不是处理缺失值
idx=pandas.to_datetime(datestrs+[None])#增加一个缺失值
print(pandas.isnull(idx))