Last updated: Apr 11, 2024
Reading time·4 min
timedelta64[ns]
column to seconds by dividingTo convert a timedelta64[ns]
column to seconds in a Pandas DataFrame
:
dt.total_seconds()
method to return the total duration of each
value in seconds.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())
Running the code sample produces the following output.
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
The DataFrame
has a task_duration
column of type timedelta64[ns]
.
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]
We used the dt.total_seconds() method to convert the column to seconds.
# 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.
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)
Running the code sample produces the following output.
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
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.
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)
Running the code sample produces the following output.
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
We converted the timedelta64[ns]
column to seconds and stored the result in
the duration_sec
column.
timedelta64[ns]
column to seconds by dividingYou can also convert a timedelta64[ns]
column to seconds by dividing.
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'))
Running the code sample produces the following output.
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
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.
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()
.
np.divide(series, np.timedelta64(1, 's'))
You can learn more about the related topics by checking out the following tutorials: