I need to subtract x months from a specific date in python and tried several ways, but get the following errors:
dates = np.array(['2023-02-28T00:00:00.000000000','2022-12-31T00:00:00.000000000','2019-01-01T00:00:00.000000000'], dtype=np.datetime64)
dates + np.timedelta64(-5,'M')
numpy.core._exceptions._UFuncInputCastingError: Cannot cast ufunc 'add' input 1 from dtype('<m8[M]') to dtype('<m8[ns]') with casting rule 'same_kind'
dates + relativedelta(months=-5)
numpy.core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('O')
datetime.fromtimestamp(dates.astype(datetime) / 1e9) + relativedelta(months=-5)
TypeError: only integer scalar arrays can be converted to a scalar index
What is the problem here?