I spent a little longer than expected figuring out how to find the midpoint of two dates for a little table of data recently. Here is a code snippet in case I (or you) have to do this again:
# midpoint of two date columns df = pd.DataFrame({'a': ['5/1/2012 0:00', '4/1/2014 0:00'], 'b': ['4/1/2014 0:00', 'unknown']}) # make time data into Timestamp format def try_totime(t): try: return pd.Timestamp(t) except: return np.nan df['start'] = df.a.map(try_totime) df['end'] = df.b.map(try_totime) # generate midpoint time # harder than it would seem... df['time'] = df.start + (df.end - df.start)/2 df
This is Awesome! I once had to do something like this in PHP and I can tell you it turned out a lot uglier than this. Python, how you so good?
🙂