Pandas: set a default datetime for None values
<p>I have a Pandas dataframe with columns that contain dates as strings (in SQL-like format). However, many cells contain <code>None</code> values. I'm trying to convert these columns to Pandas dates using <code>to_datetime</code> and set a "default" value for cells that contain the <code>None</code> value. Example code below:</p>

<pre><code>import pandas as pd

&gt;&gt;&gt; d = {'a': [1,2,3],
        'd1': ['2016-01-01','2015-10-02',None],
        'd2': [None,'2016-04-03',None]}
&gt;&gt;&gt; df = pd.DataFrame(d)
&gt;&gt;&gt; print df
   a          d1          d2
0  1  2016-01-01        None
1  2  2015-10-02  2016-04-03
2  3        None        None


&gt;&gt;&gt; date_cols = ['d1','d2']
&gt;&gt;&gt; df[date_cols] = df[date_cols].apply(pd.to_datetime)
&gt;&gt;&gt; print df
   a         d1         d2
0  1 2016-01-01        NaT
1  2 2015-10-02 2016-04-03
2  3        NaT        NaT
</code></pre>

<p>It's simple enough to convert the valid strings to a date, I just want to replace the <code>NaT</code> with the <code>default_date</code>. This is what I'd like the final dataframe to look like:</p>

<pre><code>&gt;&gt;&gt; default_date = '2015-01-01'
&gt;&gt;&gt; print df
   a         d1         d2
0  1 2016-01-01 2015-01-01
1  2 2015-10-02 2016-04-03
2  3 2015-01-01 2015-01-01   
</code></pre>

<p>use <code>fillna</code></p>

<pre><code>df[date_cols] = df[date_cols].fillna(pd.to_datetime('2015-01-01'))
df
</code></pre>

<p><a href="http://i.stack.imgur.com/EPTWy.png" rel="nofollow"><img src="http://i.stack.imgur.com/EPTWy.png" alt="enter image description here"></a></p>

python datetime pandas