Dates
Date Difference
Compute the number of days between paired start and end dates. By hand,
parse each ISO string to a datetime.date and subtract to get a
timedelta; .days extracts the whole-day count. With pandas, subtract
two datetime64 Series to get a timedelta Series, then .dt.days gives
the integer day counts.
By hand
With pandas
pd.to_datetime(df['end']) - pd.to_datetime(df['start']) subtracts the
two datetime64 columns, yielding a timedelta64 Series. .dt.days
extracts the integer day count. The result dtype is int64 — no NaN,
no float upcast.
naive.py
import datetime
starts = ['2024-01-05', '2024-03-15', '2024-06-01', '2024-09-10']
ends = ['2024-01-19', '2024-03-22', '2024-06-30', '2024-10-08']
result = []
for i in range(len(starts)):
s = datetime.date.fromisoformat(starts[i])
e = datetime.date.fromisoformat(ends[i])
result.append((e - s).days)
print('RESULT:', result)
library.py
import pandas as pd
from dalib.display import set_display
set_display()
starts = ['2024-01-05', '2024-03-15', '2024-06-01', '2024-09-10']
ends = ['2024-01-19', '2024-03-22', '2024-06-30', '2024-10-08']
df = pd.DataFrame({'start': starts, 'end': ends})
d = (pd.to_datetime(df['end']) - pd.to_datetime(df['start'])).dt.days
result = d.tolist()
print('index:', d.index.tolist())
print('dtype:', d.dtype)
print('values:', d.tolist())
print('RESULT:', result)
index: [0, 1, 2, 3]
dtype: int64
values: [14, 7, 29, 28]
RESULT: [14, 7, 29, 28]
Implementation notes
- ISO format (
YYYY-MM-DD) is used throughout to avoid locale-ambiguous parsing.pd.to_datetimehandles it unambiguously regardless of system locale; for non-ISO inputs useformat=explicitly. .dt.daysgives whole days only — the fractional-day part (from non-midnight timestamps) is discarded. Use.dt.total_seconds()and divide if sub-day precision matters.d.tolist()on anint64Series returns Pythonintvalues; naivetimedelta.daysis also a Pythonint— RESULT matches without any conversion.- Cross-reference:
diff-previous-row(python-pandas ch09) for the row-to-row lag pattern (each row vs the previous) vs the explicit paired subtraction here.