今天用tushare 导入数据库数据,tushare返回的是string类型YYmmdd格式的字符串,要转成datetime.date格式,没有找到这种转换的形式,查文档终于找到方法
def ToDate(a):
return datetime.datetime.strptime(a, '%Y%m%d').date()
a是string字符串 比如 '20200225'
datetime.datetime.strptime(a,'%Y%m%d')是将字符串a以 '%Y%m%d' 的形式转化成 datetime.datetime 格式,如果你的字符串是'2020-02-25',那么只需要改成 '%Y-%m-%d' 便能正常转化
后面的.date()是datetime.datetime对象下的方法,直接转成datetime.date格式