0

We have a dateframe, which has 2 i.a. 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.

Progman
  • 16,827
  • 6
  • 33
  • 48
Versteher
  • 23
  • 5

1 Answers1

0

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.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • Thank You. but I will have another error: strptime() argument 1 must be str, not Series. A column is Series, yes. How can I make another column that will have a difference between two other columns? thank ye – Versteher Dec 24 '20 at 13:15
  • [This](https://stackoverflow.com/questions/52633719/syntax-to-use-df-apply-with-datetime-strptime) and [this](https://stackoverflow.com/questions/22923775/calculate-pandas-dataframe-time-difference-between-two-columns-in-hours-and-minu) can help maybe. Either way, the original issue from your question seems to be resolved, consider asking another question for your pandas issue. Mark the answer as accepted to close it. @Versteher – Jarvis Dec 24 '20 at 13:17