Python
pandas
timedelta
dataframe
data manipulation

Convert timedelta to int in a pandas DataFrame

Codemia Knowledge Hub ·

Call .dt.total_seconds() on the column, decide how to handle fractions, then cast with .astype("int64"). The real decision is the unit: a duration can become seconds, minutes, hours or days, depending on what downstream code expects. Once the unit is explicit, the conversion is simple and vectorized.

A timedelta column to start from

Subtracting one timestamp column from another gives a timedelta column:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "start": pd.to_datetime(["2026-01-01 10:00:00", "2026-01-01 10:05:00"]),
5    "end": pd.to_datetime(["2026-01-01 10:03:30", "2026-01-01 10:16:30"]),
6})
7
8df["delta"] = df["end"] - df["start"]
9print(df)

df["delta"] now has a pandas timedelta dtype, not an integer dtype.

Integer seconds

python
1df["seconds_float"] = df["delta"].dt.total_seconds()
2df["seconds_int"] = df["seconds_float"].round().astype("int64")
3
4print(df[["delta", "seconds_int"]])

The design choice here is rounding. Some workflows want round, others floor, ceil or truncation. Pick one and use it consistently, because mixing rounding and truncation across pipelines creates silent disagreements between reports.

Minutes, hours or days

Other units are scaled versions of total seconds:

python
1df["minutes_int"] = (df["delta"].dt.total_seconds() / 60).round().astype("int64")
2df["hours_int"] = (df["delta"].dt.total_seconds() / 3600).round().astype("int64")
3
4print(df[["delta", "minutes_int", "hours_int"]])

For days, dividing by a Timedelta is often the most readable:

python
df["days_int"] = (df["delta"] / pd.Timedelta(days=1)).astype("int64")

No method is always best. What matters is that the target unit is obvious from both the code and the column name.

Keeping missing values (NaT)

If the column contains NaT, a direct cast to plain int64 can fail. Use the nullable Int64 dtype when missing values must survive:

python
1df2 = pd.DataFrame({"delta": [pd.Timedelta(seconds=30), pd.NaT]})
2df2["seconds"] = df2["delta"].dt.total_seconds()
3df2["seconds_nullable"] = df2["seconds"].round().astype("Int64")
4
5print(df2)

This keeps missing values instead of forcing you to fill them with a possibly misleading number.

Use .dt instead of row-wise apply

The vectorized accessor is faster and more idiomatic:

python
df["seconds"] = df["delta"].dt.total_seconds()

Avoid this:

python
# slower and usually unnecessary
# df["seconds"] = df["delta"].apply(lambda x: x.total_seconds())

Negative durations

The conversion preserves negative values:

python
df3 = pd.DataFrame({"delta": [pd.Timedelta(seconds=-45)]})
df3["seconds"] = df3["delta"].dt.total_seconds().astype("int64")
print(df3)

Negative durations can be valid, for example when measuring drift, clock skew or schedule variance. In other domains they mean bad data and should be rejected or clamped. That is a question about your data model, not about pandas.

Name the output column by its unit

duration_seconds is better than duration_int. An integer with no unit is almost useless, and once data leaves the notebook or ETL job, the column name is often the only thing preventing misinterpretation. It is also better to store one canonical unit and derive display units later than to scatter mixed-unit columns through a pipeline.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design