1

I'd like to add 1 if date_ > buy_date larger than 12 months else 0

example df

customer_id   date_         buy_date
34555        2019-01-01    2017-02-01
24252        2019-01-01    2018-02-10
96477        2019-01-01    2017-02-18

output df

customer_id   date_         buy_date    buy_date>_than_12_months
34555        2019-01-01    2017-02-01    1
24252        2019-01-01    2018-02-10    0
96477        2019-01-01    2018-02-18    1

2 Answers 2

4

Based on what I understand, you can try adding a year to buy_date and then subtract from date_ , then check if days are + or -.

 df['buy_date>_than_12_months'] = ((df['date_'] - 
                               (df['buy_date']+pd.offsets.DateOffset(years=1)))
                                  .dt.days.gt(0).astype(int))

print(df)

   customer_id      date_   buy_date  buy_date>_than_12_months
0        34555 2019-01-01 2017-02-01                         1
1        24252 2019-01-01 2018-02-10                         0
2        96477 2019-01-01 2017-02-18                         1
Sign up to request clarification or add additional context in comments.

Comments

2
import pandas as pd
import numpy as np

values = {'customer_id': [34555,24252,96477],
          'date_':  ['2019-01-01','2019-01-01','2019-01-01'],
          'buy_date':  ['2017-02-01','2018-02-10','2017-02-18'],
          }

df = pd.DataFrame(values, columns = ['customer_id', 'date_', 'buy_date'])

df['date_'] = pd.to_datetime(df['date_'], format='%Y-%m-%d')
df['buy_date'] = pd.to_datetime(df['buy_date'], format='%Y-%m-%d')

print(df['date_'] - df['buy_date'])

df['buy_date>_than_12_months'] = pd.Series([1 if ((df['date_'] - df['buy_date'])[i]> np.timedelta64(1, 'Y')) else 0 for i in range(3)])
print (df)

1 Comment

By changing condition ((df['date_'] - df['buy_date'])[i].days> 365 to ((df['date_'] - df['buy_date'])[i]> np.timedelta64(1, 'Y')) and thanks for logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.