简体   繁体   中英

Calculate and save a difference between columns with dates

We have a dateframe, which has 2 ia 2 columns with dates. We need to calculate difference between them in days.

import datetime
from datetime import date
date1 = datetime.strptime(payments['ts'], '%d/%m/%y %H:%M:%S')
date2 = datetime.strptime(payments['date'], '%d/%m/%y %H:%M:%S')
payments['delta'] = (date1 - date2).days

this gets the error: module 'datetime' has no attribute 'strptime'. What did I do wrong? Thank ye.

You need to import like this:

from datetime import date, datetime
date1 = datetime.strptime(payments['ts'], '%d/%m/%y %H:%M:%S')
date2 = datetime.strptime(payments['date'], '%d/%m/%y %H:%M:%S')

strptime belongs to the datetime class from datetime module as a class method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM