How to get inclusive difference between timestamps

Question:

I’d like to get the difference between the end and start date columns, inclusive

df = pd.DataFrame()

df['start'] = pd.to_datetime(['1/1/2020','1/2/2020'])

df['end'] = pd.to_datetime(['1/31/2020', '1/25/2020'])

df['diff'] = df['end'] - df['start']

So instead of

    start       end         diff
0   2020-01-01  2020-01-31  30 days
1   2020-01-02  2020-01-25  23 days

I want to get 31 and 24 days. I can solve it by adding a 1 day Timedelta, but it seems a bit fragile. Is there any other way>

df['diff'] = df['end'] - df['start'] + pd.Timedelta(days=1)

Asked By: meg hidey

||

Answers:

It’s exactly what you need. This is how I use it when working on dates
pd.Timedelta(days=1).

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.