简体   繁体   中英

converting columns in pandas dataframe into datetime

  gps   nt_date 
   4002  20-Dec-16 
   4002  20-Dec-16 
    C-4  30-Jan-17 
 Y21-21  17-Nov-16 
    49a  22-Dec-16 

In the dataframe above, how can I convert nt_date column to pandas datetime? I am using this but it does not work:

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

How to fix this?

You're using slightly wrong directives. Try using %b (Month as locale's abbreviated name) and %y (2 digit year, rather than %Y which is 4 digit):

df['nt_date'] = pd.to_datetime(df['nt_date'], format='%d-%b-%y')

>>> df
      gps    nt_date
0    4002 2016-12-20
1    4002 2016-12-20
2     C-4 2017-01-30
3  Y21-21 2016-11-17
4     49a 2016-12-22

For more info, you can refer to these docs

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