I'm trying to add a month to a np.datetime64:
np.datetime64('2020-01-05') + np.timedelta64(1, "M")
but this error raises:
Cannot cast ufunc 'subtract' input 1 from dtype('<m8[M]') to dtype('<m8[us]') with casting rule 'same_kind'
If I try with days or weeks, it works:
np.datetime64('2020-01-05') + np.timedelta64(1, "D")
numpy.datetime64('2020-01-06')
I read from numpy documentation that adding days and months is different (https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-and-timedelta-arithmetic), but they didn't give a solution.
NB: I'm trying NOT to use pandas or datetime, so I would like to avoid any casting
The below code works but I'm uncertain how future proof it is. I suspect converting to datetime or using pandas may in the long run be easier and more robust.
Extracting the months is from Anon's answer to getting year and month
As @pawan-jain said there are problems defining adding or subtracting months from dates. What is 2021-05-31 plus one month? 2021-06-31 isn't a date.
The code below maps
Check this meets the definition of add month that is required.