-2
ref_day = max(df['InvoiceDate']) + timedelta(days=1)
df['days_to_last_order'] = (ref_day - df['InvoiceDate']).dt.days
df.head()

I am trying to run the above code but not working. Getting error as below

TypeError: can only concatenate str (not "datetime.timedelta") to str
DataJanitor
  • 1,276
  • 1
  • 8
  • 19
  • What's not clear from the error? The column `"InvoiceDate"~ contains strings. Convert them to dates before working on them – Tomerikoo Dec 28 '22 at 08:21
  • 1
    The error isn't exactly much to go with, but I'd guess `df['InvoiceDate']` contains strings, not datetimes. – AKX Dec 28 '22 at 08:21
  • Does this answer your question? [Python date string to date object](https://stackoverflow.com/questions/2803852/python-date-string-to-date-object) – Tomerikoo Dec 28 '22 at 08:22
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 28 '22 at 10:02

1 Answers1

1

Your colum df['InvoiceDate'] contains a string, not a date in datetime format. Before running your code, you need to run pd.to_datetime(df['InvoiceDate']).

DataJanitor
  • 1,276
  • 1
  • 8
  • 19