1

I'm currently trying to check if the date time format in a certain column of a csv file is in the correct format.

I am using this:

if pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S', errors ='raise'):
    print('date format wrong')

However dates if the format %Y/%m/%d %H:%M:%S still gets through.

Any advice out there? Thanks in advance!

Added by Scott Boston:

s = pd.Series(['2020-01-01 12:01:00','2020/01/01 12:02:00'])
pd.to_datetime(s, format='%Y-%m-%d %H:%M:%S', errors='raise')

Output:

0   2020-01-01 12:01:00
1   2020-01-01 12:02:00
dtype: datetime64[ns]
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
bluefishee
  • 11
  • 2
  • _However dates if the format %Y/%m/%d %H:%M:%S still gets through._ Please provide a [mcve]. – AMC Jun 03 '20 at 01:58
  • 6/3/2020 2:30:19 PM, for example in my csv file does not throw any errors – bluefishee Jun 03 '20 at 02:29
  • That is not enough to form a [mcve], there are easily a million things in the program which could lead to this issue. – AMC Jun 03 '20 at 02:30
  • you have messed up your parentheses. Anyway, that does not seem like the right way to check for multiple date types. I would try something like this post: https://stackoverflow.com/questions/26920871/handling-pandas-dataframe-columns-with-mixed-date-formats As AMC said, people could probably help you within minutes if you simply could create a minimal reproducible example – David Erickson Jun 03 '20 at 02:35
  • Hrm.. interesting... That is unexpected, however, why wouldn't you want it to interpret both as dates? – Scott Boston Jun 03 '20 at 02:56

0 Answers0