user12625679
user12625679

Reputation: 696

Pandas df comparing two dates condition

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

Upvotes: 1

Views: 109

Answers (2)

Davinder Singh
Davinder Singh

Reputation: 2162

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)

Upvotes: 2

anky
anky

Reputation: 75150

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

Upvotes: 4

Related Questions