简体   繁体   中英

Problem converting column with date info as object to datetime

I've a column with birth dates as object, the problem is when I tried to convert it into datetime, because it displays always the next warning

time data '27126' does not match format '%d/%m/%Y' (match)

         date
0        05/06/1980
1        31/07/1947
2        07/01/1963
3        26/03/1973
4        30/01/1991
5        12/12/1991
6        13/08/1987
7        10/01/1944
8        23/06/1965
9        08/10/1995

till now I've tried the next codes:

df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')

df['date'] = df['date'].apply(lambda x: datetime.datetime.strptime(x, "%d/%m/%Y").strftime("%Y-%m-%d"))

df['date'] = pd.to_datetime(df['date'].str.strip(), format='%d/%m/%Y')

添加参数errors='coerce'以将不匹配的日期时间转换为缺失值,此处为NaT

df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y', errors='coerce')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM