29

I have a date column (called 'Time') which contains days/hours/mins etc (timedelta). I have created a new column in my dataframe and I want to create a new column with the 'Time' column converted into seconds.

I did find how to convert a column. How can I create a new one from a the conversion in seconds of an exising timedelta column ?

endive1783
  • 827
  • 1
  • 8
  • 18
Nicholas
  • 3,517
  • 13
  • 47
  • 86

2 Answers2

57

I think you need total_seconds:

print (df['col'].dt.total_seconds())

Sample:

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)
       date1               date2
0 2015-01-01 2015-01-01 02:00:00
1 2015-01-02 2015-01-02 01:00:00
2 2015-01-03 2015-01-03 00:00:00

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

print (df)
       date1               date2     diff  seconds
0 2015-01-01 2015-01-01 02:00:00 02:00:00   7200.0
1 2015-01-02 2015-01-02 01:00:00 01:00:00   3600.0
2 2015-01-03 2015-01-03 00:00:00 00:00:00      0.0

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

print (df)
       date1               date2    diff
0 2015-01-01 2015-01-01 02:00:00  7200.0
1 2015-01-02 2015-01-02 01:00:00  3600.0
2 2015-01-03 2015-01-03 00:00:00     0.0

If need cast to int:

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

print (df)
       date1               date2  diff
0 2015-01-01 2015-01-01 02:00:00  7200
1 2015-01-02 2015-01-02 01:00:00  3600
2 2015-01-03 2015-01-03 00:00:00     0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you Jezreal. Exactly what I needed :)... I spend so much time googling, I don't know why I don't head here straight away. I guess it helps me with the learning process x) – Nicholas Dec 06 '16 at 10:42
  • 2
    not working for me. Here is the error AttributeError: 'DatetimeProperties' object has no attribute 'total_seconds' – Krissh Jan 15 '20 at 06:24
  • 1
    @Krissh - You have to use `total_seconds` with difference column filled by timedeltas, not with datetimes column like `df['date1'].dt.total_seconds()` – jezrael Jan 15 '20 at 06:37
7

Let's assume your DataFrame's name is df.

If you want to create a new column with the seconds you should do the following:

df['newColumn'] = df['Time'].dt.total_seconds()
amin
  • 1,413
  • 14
  • 24