Shanoo
Shanoo

Reputation: 1245

Find number of months between two dates in python

I have a date object and a date column 'date1' in pandas dataframe 'df' as below:

date = '202107'

df
    date1 
0   2021-07-01   
1   2021-08-01   
2   2021-09-01   
3   2021-10-01   
4   2021-11-01
5   2023-02-01
6   2023-03-01

I want to create a column 'months' in df where

months = (date1 + 1month) - date

My output dataframe should look like below:

df
    date1        months
0   2021-07-01   1
1   2021-08-01   2
2   2021-09-01   3
3   2021-10-01   4
4   2021-11-01   5
5   2023-02-01   20
6   2023-03-01   21

Upvotes: 2

Views: 4338

Answers (3)

flying_fluid_four
flying_fluid_four

Reputation: 764

Given a date variable as follows

mydate = 202003

and a dataframe [df] containing a datetime variable start_date. You can do:

mydate_to_use= pd.to_datetime(mydate , format = '%Y%m', errors='ignore')

df['months'] = (df['START_DATE'].dt.year - mydate_to_use.year) * 12 + (df['START_DATE'].dt.month - mydate_to_use.month)

Upvotes: 1

BENY
BENY

Reputation: 323226

IIUC

s=(df.date1-pd.to_datetime(date,format='%Y%m'))//np.timedelta64(1, 'M')+1
Out[118]: 
0    1
1    2
2    3
3    4
4    5
Name: date1, dtype: int64
df['months']=s

Update

(df.date1.dt.year*12+df.date1.dt.month)-(pd.to_numeric(date)//100)*12-(pd.to_numeric(date)%100)+1
Out[379]: 
0     1
1     2
2     3
3     4
4     5
5    20
6    21
Name: date1, dtype: int64

Upvotes: 0

YOLO
YOLO

Reputation: 21709

Here's a way to do using pandas:

date = '202107'
date = pd.to_datetime(date, format='%Y%m')

df['months'] = (df.date + pd.offsets.MonthBegin(1)).dt.month - date.month

print(df)

        date  months
0 2021-07-01       1
1 2021-08-01       2
2 2021-09-01       3
3 2021-10-01       4
4 2021-11-01       5

Upvotes: 1

Related Questions