對應的今年數據顯示為06.14,去年以及前年數據顯示為2021.12.27
month=['01','02','03','04','05','06','07','08','09','10','11','12']
date_pre=[]
for i in df['date']:
if i.split('.')[0] in month:
i='2022.'+i
i=i.replace('.','-')
# print(i) #str 2022-06-12
i=datetime.date(*map(int,i.split('-')))
date_pre.append(i)
else:
i=i.replace('.','-')
i=datetime.date(*map(int,i.split('-')))
date_pre.append(i)
df['date_pre']=pd.DataFrame(data=date_pre)
形如: 2019-03-14 <class 'datetime.date'>
python將字符串類型改成日期類型_Python 學習者的博客-CSDN博客_python字符串轉日期格式