Data
I have the following data:
data = [['1987-09-01', 5], ['1987-09-01', 2.66], ['1987-09-01', np.nan]]
df = pd.DataFrame(data, columns=['Date', 'year'])
df['Date'] = pd.to_datetime(df['Date'])
Goal
To subtract the number of years from the date. For np.nan, I do not want any value to be subtracted.
Attempt
My attempt is as follows:
df['Date'] - pd.to_timedelta(df.year.astype(str), units = 'Y')
Which leads to the following error:
ValueError: no units specified
I know that the number of years is not supported in pd.to_timedelta. I was wondering how I can accomplish my goal in another way?