1

I want to place a new value on new column based on a condition like this:

temp['month'] = diff_month(datetime.now() if temp['endda_x'].astype('str').str.startswith('9999') else pd.to_datetime(temp['endda_x']), pd.to_datetime(temp['begda_x']))

so, if column endda_x starts with '9999' the month difference will calculated by today's date. but if it doesn't, the month difference will be calculated by that date.

but the error occurs:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I know that error but I am not really sure if it is in this case. I'll appreciate the answer, Thank you

Mark
  • 7,785
  • 2
  • 14
  • 34
biyazelnut
  • 256
  • 7
  • 2
    `temp['month'] = diff_month(np.where(temp['endda_x'].astype('str').str.startswith('9999'), datetime.now(), pd.to_datetime(temp['endda_x'])), pd.to_datetime(temp['begda_x']))` – jezrael Aug 16 '23 at 05:18
  • my endda_x is '999-12-13' but why its causing error : OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 9999-12-31 00:00:00 (error at pd.to_datetime(temp['endda_x'])) where it should going with datetime.now() since its startswith 9999? – biyazelnut Aug 16 '23 at 09:09
  • Use `pd.to_datetime(temp['endda_x'], errors='coerce')` – jezrael Aug 16 '23 at 09:59
  • @jezrael with that my endda_x changed to NaT right? is there any way to keep it as it is? – biyazelnut Aug 16 '23 at 10:02

0 Answers0