pythonpython-datetime

How to convert timedelta (with days) to total seconds


I need to covert Time Elapsed to total seconds. I used three functions with no luck.

First function:

ny["Time Elapsed"].time()

AttributeError: 'Series' object has no attribute 'time'

Second function:

ny["Time Elapsed"].total_seconds()

AttributeError: 'Series' object has no attribute 'total_seconds'

Third function:

ny["Time Elapsed"](int)

TypeError: 'Series' object is not callable

a picture of the dataframe is attached. enter image description here

I am trying to convert the time duration in the time elapsed column to seconds. The name of the dataframe is "ny".


Solution

  • Your problem arises because you are not using the .dt accessor. You should use:

    ny['tot_s'] = ny["Time Elapsed"].dt.total_seconds()