Convert timedelta64 column to Seconds in Pandas DataFrame

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. Convert timedelta64 column to Seconds in Pandas DataFrame
  2. Convert timedelta64[ns] column to seconds by dividing

# Convert timedelta64 column to Seconds in Pandas DataFrame

To convert a timedelta64[ns] column to seconds in a Pandas DataFrame:

  1. Use square brackets to access the specific column.
  2. Use the dt.total_seconds() method to return the total duration of each value in seconds.
main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'job': ['dev', 'web dev', 'accountant', 'dev'], 'task_duration': [ np.timedelta64(1, "D"), np.timedelta64(30, "m"), np.timedelta64(90, "s"), np.timedelta64(1, "h") ], }) print(df) print('-' * 50) print(df['task_duration'].dt.total_seconds())
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name job task_duration 0 Alice dev 1 days 00:00:00 1 Bobby web dev 0 days 00:30:00 2 Carl accountant 0 days 00:01:30 3 Dan dev 0 days 01:00:00 -------------------------------------------------- 0 86400.0 1 1800.0 2 90.0 3 3600.0 Name: task_duration, dtype: float64

convert timedelta64 ns column to seconds in dataframe

The DataFrame has a task_duration column of type timedelta64[ns].

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'job': ['dev', 'web dev', 'accountant', 'dev'], 'task_duration': [ np.timedelta64(1, "D"), np.timedelta64(30, "m"), np.timedelta64(90, "s"), np.timedelta64(1, "h") ], }) # name job task_duration # 0 Alice dev 1 days 00:00:00 # 1 Bobby web dev 0 days 00:30:00 # 2 Carl accountant 0 days 00:01:30 # 3 Dan dev 0 days 01:00:00 print(df) print(df['task_duration'].dtype) # 👉️ timedelta64[ns]
The code for this article is available on GitHub

We used the dt.total_seconds() method to convert the column to seconds.

main.py
# 0 86400.0 # 1 1800.0 # 2 90.0 # 3 3600.0 # Name: task_duration, dtype: float64 print(df['task_duration'].dt.total_seconds())

The method returns the total duration of each value in seconds.

Make sure to access the .dt namespace on the column before calling total_seconds().

If you need to convert the column in seconds and update the DataFrame, use bracket notation.

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'job': ['dev', 'web dev', 'accountant', 'dev'], 'task_duration': [ np.timedelta64(1, "D"), np.timedelta64(30, "m"), np.timedelta64(90, "s"), np.timedelta64(1, "h") ], }) print(df) print('-' * 50) df['task_duration'] = df['task_duration'].dt.total_seconds() print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name job task_duration 0 Alice dev 1 days 00:00:00 1 Bobby web dev 0 days 00:30:00 2 Carl accountant 0 days 00:01:30 3 Dan dev 0 days 01:00:00 timedelta64[ns] -------------------------------------------------- name job task_duration 0 Alice dev 86400.0 1 Bobby web dev 1800.0 2 Carl accountant 90.0 3 Dan dev 3600.0

convert timedelta64 column to seconds and update dataframe

The code sample updates the task_duration column from timedelta64[ns] to seconds in place.

You can also add a new column that stores the duration in seconds.

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'job': ['dev', 'web dev', 'accountant', 'dev'], 'task_duration': [ np.timedelta64(1, "D"), np.timedelta64(30, "m"), np.timedelta64(90, "s"), np.timedelta64(1, "h") ], }) print(df) print('-' * 50) df['duration_sec'] = df['task_duration'].dt.total_seconds() print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name job task_duration 0 Alice dev 1 days 00:00:00 1 Bobby web dev 0 days 00:30:00 2 Carl accountant 0 days 00:01:30 3 Dan dev 0 days 01:00:00 -------------------------------------------------- name job task_duration duration_sec 0 Alice dev 1 days 00:00:00 86400.0 1 Bobby web dev 0 days 00:30:00 1800.0 2 Carl accountant 0 days 00:01:30 90.0 3 Dan dev 0 days 01:00:00 3600.0

convert timedelta64 ns column to seconds add new column

We converted the timedelta64[ns] column to seconds and stored the result in the duration_sec column.

# Convert timedelta64[ns] column to seconds by dividing

You can also convert a timedelta64[ns] column to seconds by dividing.

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'job': ['dev', 'web dev', 'accountant', 'dev'], 'task_duration': [ np.timedelta64(1, "D"), np.timedelta64(30, "m"), np.timedelta64(90, "s"), np.timedelta64(1, "h") ], }) print(df) print('-' * 50) print(df['task_duration'] / np.timedelta64(1, 's'))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name job task_duration 0 Alice dev 1 days 00:00:00 1 Bobby web dev 0 days 00:30:00 2 Carl accountant 0 days 00:01:30 3 Dan dev 0 days 01:00:00 -------------------------------------------------- 0 86400.0 1 1800.0 2 90.0 3 3600.0 Name: task_duration, dtype: float64

convert timedelta64 column to seconds by dividing

We simply divided the values in the task_duration column by a timedelta64 object that represents 1 second.

This works in the same way, however, is a bit more implicit than using dt.total_seconds().

If you're working with a Pandas Series object, you can use the following code sample.

main.py
series.apply(lambda x: x / timedelta64(1, 's'))

The Series.apply() method invokes a function on the values of a Series object.

If you don't want to use the Series.apply() method, use numpy.divide().

main.py
np.divide(series, np.timedelta64(1, 's'))

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.