I have a dataset where I would like to create a new column that contains the month difference using dates.
Data
reg in
9/1/2021 9/30/2021
9/8/2021 11/22/2021
9/8/2021 1/13/2022
10/1/2021 10/15/2021
11/2/2022 5/1/2023
Desired
reg in new_month
9/1/2021 9/30/2021 0
9/8/2021 11/22/2021 2
9/8/2021 1/13/2022 4
10/1/2021 10/15/2021 0
11/2/2022 5/1/2023 6
Doing
from datetime import datetime
df['new_month'] = df['in'].dt.to_period('M').astype(int) -
df['reg'].dt.to_period('M').astype(int)
However I wish for the output to be in whole numbers, and if it is under a month, the result will be 0. Any suggestion is appreciated
CodePudding user response:
try the following
df['new_column'] = (df.reg - df.in)
CodePudding user response:
I think the best way to do this is to start by finding the difference of the days between the two dates and then perform floor division by 30 to get the amount of months.
from datetime import datetime
def days_between(d1, d2):
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
x = abs((d2 - d1).days)
return x//30