Compute the whole-day duration between two pandas datetime columns
Quick Overview
Write a pandas function that returns the whole number of days between two datetime columns as an integer Series named days. It tests timedelta arithmetic, truncation of partial days, the dtype and index of the result, and how missing dates change that dtype.
Compute the whole-day duration between two pandas datetime columns
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def duration_days(df: pd.DataFrame) -> pd.Series`. `df` has two `datetime64[ns]` columns, `'start'` and `'end'` (`end` is guaranteed to be at or after `start`). Return the whole-day duration per row.
The result must meet these requirements:
- Compute `(df['end'] - df['start']).dt.days` (whole days, truncated, not rounded).
- Return an `int` Series named `'days'` with the same index as `df`.
Example:
```python
df = pd.DataFrame({
"start": pd.to_datetime(["2021-01-01 00:00", "2021-01-01 08:00", "2021-03-05 00:00"]),
"end": pd.to_datetime(["2021-01-04 00:00", "2021-01-03 07:59", "2021-03-05 00:00"]),
})
duration_days(df)
# 0 3
# 1 1
# 2 0
# Name: days, dtype: int64
```
Row 0 spans exactly three days. Row 1 spans 1 day 23 hours 59 minutes, which truncates to 1. Row 2 starts and ends at the same moment, so its duration is 0.
```hint Partial days
Work out what happens to the hours and minutes left over after the last full day, and check what name the subtraction result carries before you return it.
```
### Constraints & Assumptions
- `start` and `end` are both `datetime64[ns]`.
- `end` is at or after `start` in every row, so every duration is zero or positive.
### What a Strong Answer Covers
- Subtracting the columns to get a timedelta Series and taking its whole-day component.
- Why the result is truncated whole days, shown on an interval with a partial day.
- The dtype, name and index of the returned Series.
- What a missing date in either column would do to the dtype.
### Follow-up Questions
- How would `.dt.days` behave if you needed fractional days instead?
- What happens if either column contains `NaT`?
- How would you count the calendar dates between start and end (23:00 to 01:00 the next day counts as 1) instead of elapsed whole days?
Overview: Write a pandas function that returns the whole number of days between two datetime columns as an integer Series named days. It tests timedelta arithmetic, truncation of partial days, the dtype and index of the result, and how missing dates change that dtype.
Implement def duration_days(df: pd.DataFrame) -> pd.Series. df has two datetime64[ns] columns, 'start' and 'end' (end is guaranteed to be at or after start). Return the whole-day duration per row.
The result must meet these requirements:
Compute
(df['end'] - df['start']).dt.days
(whole days, truncated, not rounded).
Return an
int
Series named
'days'
with the same index as
df
.
Row 0 spans exactly three days. Row 1 spans 1 day 23 hours 59 minutes, which truncates to 1. Row 2 starts and ends at the same moment, so its duration is 0.
Constraints & Assumptions
start
and
end
are both
datetime64[ns]
.
end
is at or after
start
in every row, so every duration is zero or positive.
What a Strong Answer Covers Guidance
Subtracting the columns to get a timedelta Series and taking its whole-day component.
Why the result is truncated whole days, shown on an interval with a partial day.
The dtype, name and index of the returned Series.
What a missing date in either column would do to the dtype.
Follow-up Questions Guidance
How would
.dt.days
behave if you needed fractional days instead?
What happens if either column contains
NaT
?
How would you count the calendar dates between start and end (23:00 to 01:00 the next day counts as 1) instead of elapsed whole days?