When I calculated the days difference with Python, the result was not integer type but timedelta type. It would not calculate with integer type variable, so I researched method for conversion variables from timedelta type to integer type. I introduce this program and the program of calculation timedelta type variables without conversion.
Please note that
– this may not be the best method.
– I am not good at English. So this text may have incorrect English.
execution environment
Python3 (3.7.4)
Jupyter Lab version 1.1.4
Windows 10 (64bit)
Import librarys
The following program imports the necessary libraries. This time, we make the date created as a character string type in the data frame, assuming the imported the CSV file.
import pandas as pd from pandas import DataFrame, Series import datetime from datetime import timedelta
The variable created as a character type in the data frame.
df = pd.DataFrame({ 'DateA' : ['2020-01-15','2020-01-15','2020-01-15'],
'DateB' : ['2020-01-15','2020-01-01','2020-01-31']})
df.head()

Convert date variable from string type to datetime type so that it can be calculated.
df["DateA2"]=pd.to_datetime(df["DateA"]) df["DateB2"]=pd.to_datetime(df["DateB"]) df.head()

df.dtypes

Culculate the defference of dates.
If subtruct each datetime type variables, result is made as a timedelta type.
df["Days"]=df["DateA2"]-df["DateB2"] df.head()

df.dtypes

The timedelta type variable can not calculate with integer type.
df["Days2"] = df["Days"] + 1

Convertion from timedelta type to integer type.
The time variable can convert to integer type by dividision by timedelta(days=1).
df["Days_Int"] = (df["Days"] / timedelta(days=1))
Calculation the timedelta variables without conversion.
Using timedelta(days=1), you can calculate timedelta type variables without conversion.
IntVar = 2 df["Days_Plus1"] = df["Days"] + timedelta(days=1) * IntVar df.head()

Thank you.