building datetime from 3 integer/float columns in pandas
<p>After loading a DataFrame with Panda (from a csv) with this structure:</p>

<pre><code>            startmonth  startday  startyear  endmonth  endday  endyear  
caseid                                                                   
1945121601        12.0      16.0       1945       5.0    27.0   1947.0   
1946031101         3.0      11.0       1946      10.0     9.0   1993.0   
1946110101        11.0       1.0       1946       2.0     4.0   1947.0   
</code></pre>

<p>I am thinking how to efficiently use the first 3 &amp; last 3 columns to generate 2 datetime columns, say 'startdate' and 'enddate.' Since there are missing values that need to be dealt with, the parse_dates &amp; date_parser arguments in read_csv seem a bit unwieldy, so I wrote the function below.</p>

<p>First, I fill the NaN value so as to cast month and date from float to integer, and then string them together for parsing.</p>

<pre><code>def dateparser(y=df.startyear,m=df.startmonth,d=df.startday):
    m = m.fillna(1).astype(int)
    d = d.fillna(1).astype(int)
    x = str(y) + " " + str(m) + " " + str(d)
    return pd.datetime.strptime(x, '%Y %m %d')
</code></pre>

<p>The resulted error message is a bit confusing, as the string format should be exactly the same with what strptime expect.</p>

<pre><code>n [338]: dateparser()
Traceback (most recent call last):

  File "&lt;ipython-input-338-917257f547ca&gt;", line 1, in &lt;module&gt;
    dateparser()

  File "&lt;ipython-input-337-41aa89124ae6&gt;", line 5, in dateparser
    return pd.datetime.strptime(x, '%Y %m %d')

  File "/Users/Username/anaconda/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)

  File "/Users/Username/anaconda/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))

    ValueError: time data 'caseid\n1945121601    1945\n1946031101    
1946\n1946110101    1946\n1947022401    1947\n1947053101
        1947\n1947111001    1947\n1947120501    1947\n1947120502    
1947\n1947120503    1947\n1947120504    1947\n1947120505    
1947\n1947120506    1947\n1947120507    1947\n1947122001    
1947\n1948032501    1948\n1948032502    1948\n1948070101       
6\n2005100601    10\n

    Name: startmonth, dtype: int64 caseid\n1945121601    16\n1946031101    
6\nName: startday, dtype: int64' does not match format '%Y %m %d'`
</code></pre>

<p>I also tried another parsing package that turns most of the datetime string in English language into datetime variable without issue:</p>

<pre><code>from dateutil.parser import parse
def dateparser():
    (same function as above)
    return parse(x)
</code></pre>

<p>And it also results in error (ValueError: Unknown string format)...</p>

<p>Any thoughts on how to improve the function are much appreciated. It is also a bit strange for me that most package functions only converts string to datetime, and one needs to make integers/floats into string even if it shouldn't be that hard to directly convert numerical data into datetime formats... did I miss some obvious solutions?</p>

<p>Although not absolutely sure, the issue seems to be that I am trying to feed the parser a pandas Series while they expect only to take string.</p>

<p>In this cae, Panda's own to_datetime function can do the work.</p>

<pre><code>def dateparser(y=t4.startyear,m=t4.startmonth,d=t4.startday):
    y = y.astype(str)     
    m = m.fillna(1).astype(int).astype(str)
    d = d.fillna(1).astype(int).astype(str)
    x = y +' '+ m +' '+ d
    return pd.to_datetime(x)
</code></pre>

python string datetime pandas