1

I am trying to get an age and thus substracting the today's date from a date in a data frame. So trying to do this:

(df
.assign(dob = lambda x:  pd.to_datetime(x.date,format='%Y%m%d'))
.assign(age = lambda x: x.dob - pd.datetime.now())
)

but I get the following error and don't see why

OverflowError: Overflow in int64 addition

noah
  • 2,616
  • 13
  • 27
corianne1234
  • 634
  • 9
  • 23
  • the assign/lambda construct seems a bit hard to read to me - why no simply assign new columns the "conventional way"? e.g. `df['dob'] = pd.to_datetime(df['date'])` and `df['age'] = pd.Timestamp('now') - df['dob']`. – FObersteiner Oct 14 '20 at 17:54
  • btw. [here](https://stackoverflow.com/q/51491893/10197418) are some more ideas how to approach this. – FObersteiner Oct 14 '20 at 18:02

1 Answers1

0

This is caused by dob having only date and now() having date and time. pd.datetime.now().strftime('%Y%m%d') should fix it. It is formatting it in the same way your dob is formatted.

noah
  • 2,616
  • 13
  • 27
  • Unfortunately no. I tried this too and I get this error ```TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'``` – corianne1234 Oct 15 '20 at 07:35