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:
df["delta"] now has a pandas timedelta dtype, not an integer dtype.
Integer seconds
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:
For days, dividing by a Timedelta is often the most readable:
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:
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:
Avoid this:
Negative durations
The conversion preserves negative values:
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.
Related reading
- python dataframe pandas drop column using int
- Python float argument must be a string or a number, not ''pandas._libs.interval.Interval''
- Python Graph Library
- Python Implementation of OPTICS Clustering Algorithm
- Python Create unix timestamp five minutes in the future
- Python creating a dictionary of lists
- Python implementation of the Wilson `Score` Interval?
- Python in R - Error could not find a Python environment for /usr/bin/python
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.