Try Except block not working with datetime object?
<p>I have a DataFrame with some datetime data in one column and whatever else in other columns. However, some of the data is messed up, e.g.:</p>

<pre><code>11/11/2014 22:28    15.1
11/11/2014 22:29    16.1
11/11/2014 22:30    15.2
bollocks            10000
11/11/2014 22:32    15.4
:00                 
11/11/2014 22:34    15.3
</code></pre>

<p>I would like to get rid of the lines that are messed up. For now, I decided to just replace them with NaN values (but dropping them would also help, only it didn't work in the cycle so it's not an issue, in the next step I can just use <code>dropna()</code>). I'm doing this using <code>try()</code>, but the exception doesn't work. My code looks like this:</p>

<pre><code>for line in df.ix[:,"DATETIME"]:    
    try:
        line = datetime.datetime.strptime(line,"%d/%m/%Y %H:%M")
    except ValueError:
        line = 'NaN'
    except TypeError:
        line = 'NaN'
</code></pre>

<p>But in the end, I still get the <code>ValueError: time data '156004E00F455AA' does not match format '%d/%m/%Y %H:%M'</code> and the faulty lines are not replaced with <code>NaN</code>. What is wrong here? 
(I also tried putting the errors on one line like this: <code>except (ValueError, TypeError):</code> and it didn't work either...)</p>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html" rel="nofollow"><code>pd.to_datetime</code></a> can set to <code>NaT</code> ill-formed data while converting the column to datetime. </p>

<pre><code>pd.to_datetime(df['DATETIME'], format = '%d/%m/%Y %H:%M', errors='coerce')

           DATETIME
0  11/11/2014 22:28
1  11/11/2014 22:29
2  11/11/2014 22:30
3               NaT
4  11/11/2014 22:32
5               NaT
6  11/11/2014 22:34
</code></pre>

python datetime pandas