Reputation: 31
Trying to create a new column in a dataframe that shows number of days between now and a past date. So far I have below code but it returns 'days' + a timestamp. How can I get just the number of days?
import pytz
now = datetime.datetime.now(pytz.utc)
excel1['days_old'] = now - excel1['Start Time']
Returns:
92 days 08:08:06.667518
Upvotes: 0
Views: 79
Reputation: 31
Also worked for me
import datetime
import pytz
now = datetime.datetime.now(pytz.utc)
excel1['days_old'] = (now - excel1['Start Time']).astype('timedelta64[D]')
Upvotes: 0
Reputation: 30971
Assuming that Start Time column is of datetime type, run:
(pd.Timestamp.now() - df['Start Time']).dt.days
Upvotes: 1
Reputation: 1991
excel1['days_old'] will hold "timedeltas". To get them to the day difference, just use ".days" like this:
import pytz
now = datetime.datetime.now(pytz.utc)
excel1['days_timedelta'] = now - excel1['Start Time']
excel1['days_old'] = excel1['days_timedelta'].days
Upvotes: 1