wawawa
wawawa

Reputation: 3345

How to calculate how many days from today's date based on a column that contains dates

I have a dataframe, one column contains multiple dates called 'date', I want to convert it to the days calculated from today's date (2020-07-04), here's the code:

profile['membership_date'] = pd.to_datetime(profile['became_member_on'].astype(str), format='%Y%m%d')

the column is like this :

0       2017-02-12
1       2017-07-15
2       2018-07-12
3       2017-05-09
4       2017-08-04
5       2018-04-26

Then we get today's date:

today_date = datetime.date.today().strftime('%Y-%m-%d')

#calculate days, I tried two different ways but still getting error

profile['membership_date'] - today_date

profile['membership_days'] = (profile['membership_date'] - today_date).days

#error:unsupported operand type(s) for -: 'DatetimeIndex' and 'str'

Can someone help me, thanks.

Upvotes: 1

Views: 186

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71689

Use, Series.sub to subtract the membership_date column from the pd.Timestamp.now which returns a series with timedelta objects, finally use Series.dt.days to get the integer representation of number of days elapsed between two dates.

profile['membership_days'] = (
    profile['membership_date'].sub(pd.Timestamp.now()).dt.days
)

Result:

# print(profile)
        Date  membership_days
0 2017-02-12            -1239
1 2017-07-15            -1086
2 2018-07-12             -724
3 2017-05-09            -1153
4 2017-08-04            -1066
5 2018-04-26             -801

Upvotes: 1

Related Questions