Python date range within 12 months

Issue

I want to create a new column if survey_start date is with 12 months prior to start_date then 1 else 0

ID survey_date start_date
1 2019-05-01 2022-05-01
1 2021-05-01 2022-05-01
1 2023-05-01 2022-05-01
1 2021-12-01 2022-05-01
2 2010-01-01 2010-02-01
2 2011-05-01 2010-02-01

output

ID survey_date start_date new_column
1 2019-05-01 2022-05-01 0
1 2021-05-01 2022-05-01 1
1 2023-05-01 2022-05-01 0
1 2021-12-01 2022-05-01 1
2 2010-01-01 2010-02-01 1
2 2011-05-01 2010-02-01 0

How do I do this in pandas dataframe?

Solution

here is one way to do it using np.where

df['new_col'] = np.where(((df['start_date'].astype('datetime64') - df['survey_date'].astype('datetime64') ).dt.days).between(0,366),1, 0)
df
    ID  survey_date     start_date  new_col
0   1   2019-05-01      2022-05-01   0
1   1   2021-05-01      2022-05-01   1
2   1   2023-05-01      2022-05-01   0
3   1   2021-12-01      2022-05-01   1
4   2   2010-01-01      2010-02-01   1
5   2   2011-05-01      2010-02-01   0

Answered By – Naveed

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.