Panda: datetime to seconds

From OnnoWiki
Jump to navigation Jump to search


import pandas as pd
import datetime as dt

df = pd.DataFrame({'date1':pd.date_range('2015-01-01', periods=3),
                   'date2':pd.date_range('2015-01-01 02:00:00', periods=3, freq='23H')}) 
print (df)


df['diff'] = df['date2'] - df['date1']
df['seconds'] = df['diff'].dt.total_seconds()
print (df)
print df['diff'].dt.total_seconds()


df['diff'] = df['date2'] - df['date1']
df['diff'] = df['diff'].dt.total_seconds().astype(int)