Shanoo :
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
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
YOBEN_S :
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