Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add timedelta to date column based on conditions

Here is my sample data

        Date    tf  tf_int
0   2022-01-01  D   1
1   2022-01-02  W   3
2   2022-01-03  M   2
3   2022-01-04  Y   2

I want to make a new column based that will add the required timedelta to the Date column.

For eg, for my first row, I want to add 1 day to the date (2022-1-1) so the result would be 2022-1-2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any vectorised way to achieve this ?
I don’t using multiple .loc[] calls to accomplish this separately for days,weeks, months etc; But I would prefer the solution to be vectorised

My unsuccessful attempt with .loc[]

df.loc[df['tf'] =='D','publish_date'] = df['Date'].dt.date + dt.timedelta(days = df['tf_int'])

But this results in error because df[‘tf_int’] is a series and not an exact value

Edit:
For my use case, adding one month will add affect only the month component, and not do anything to the date

>Solution :

If need exact year and months is necessary use list comprhension solution with offsets.DateOffset:

d = {'D':'days','W':'weeks', 'M':'months', 'Y':'years'}

df['publish_date'] = [z + pd.offsets.DateOffset(**{d[y]: x}) 
                     for x, y, z in zip(df['tf_int'], df['tf'], df['Date'].dt.normalize())]
print (df)
        Date tf  tf_int publish_date
0 2022-01-01  D       1   2022-01-02
1 2022-01-02  W       3   2022-01-23
2 2022-01-03  M       2   2022-03-03
3 2022-01-04  Y       2   2024-01-04
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading