bennyrjz
bennyrjz

Reputation: 13

Add a Month Column to a Date Column

I'm working with a dataframe that contains the next columns

DATE       Months  New_Date
2020-07-03    1    2020-08-01
2022-05-06    6    2022-11-01
2020-06-23    4    2020-10-01

I need to add the values (months) in the column 'months' to the column date and get a new date. I've been researching and find some solutions like this

self.df['New_Date'] = (
self.df["DATE"].values.astype("datetime64[M]") 
+ self.df["Months"].values.astype("timedelta64[M]")
)

But as you can see, I get the correct month but the day it's the first of that month and not the correct day of the date.

I am trying to not to use apply & offset or apply & relativedelta cause I think that the efficiency of my script won't be good. So I want to know if exists a way to use relativedelta or offset without using apply (maybe vectorizing but I couldn't figure out how to) or if you know a different way to get the correct result.

Thanks in advance, I really appreciate your help!

Upvotes: 0

Views: 795

Answers (1)

SomeDude
SomeDude

Reputation: 14238

You are probably looking for:

df['DATE'] = pd.to_datetime(df['DATE'])
tdf = np.vectorize(lambda x:np.timedelta64(x,'M'))
df['NewDate'] = (df['DATE'] + tf(df['Months'])).dt.date

Upvotes: 1

Related Questions