0

'20180227' how i can convert this to date format in python

Tried with this , let suppose we similar string in entire columns of pandas data frame.

pd.to_datetime(df['colaname'])

But showing me 1970 date which is default . Any solution on this please guide

Nabi Shaikh
  • 787
  • 1
  • 6
  • 26

1 Answers1

1

Add parameter format for YYYYMMDD - %Y%m%d:

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

If possible some values not match this format and need convert them to NaT:

pd.to_datetime(df['colaname'], format='%Y%m%d', errors='coerce')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252