I have below pandas data frame
import pandas as pd
import numpy as np
dat = pd.DataFrame({'A' : [1,2,3,4,5], 'B' : ['2002-01-01', '2003-01-01', '2004-01-01', '2004-01-01', '2005-01-01']})
dat['A'] = dat['A'].astype('Int64')
Now I want to create another date column from column B by adding number of months from column A. Below is my code
pd.to_datetime(dat['B'], errors = 'coerce', dayfirst = True) + pd.DateOffset(months = dat['A'])
However with that, I get below error,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "offsets.pyx", line 1382, in pandas._libs.tslibs.offsets.RelativeDeltaOffset.__init__
File "offsets.pyx", line 328, in pandas._libs.tslibs.offsets._determine_offset
File "/Users/abc/Python_VENV/lib/python3.12/site-packages/dateutil/relativedelta.py", line 172, in __init__
if any(x is not None and x != int(x) for x in (years, months)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/abc/Python_VENV/lib/python3.12/site-packages/dateutil/relativedelta.py", line 172, in <genexpr>
if any(x is not None and x != int(x) for x in (years, months)):
^^^^^^
File "/Users/abc/Python_VENV/lib/python3.12/site-packages/pandas/core/series.py", line 248, in wrapper
raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'int'>
Could you please help to resolve this?