Search code examples
pythonpython-3.xdatetimetimedelta

How to subtract using datetime in py


I have a dataset and would like to create a new column that looks 90 days back from the start_Date column. I have tried two ways and both cause a typeError.

df['prior_date'] = df['start_Date'] - 90

And I tried

df['prior'] = (df['start_Date')] - dt(days = 90)).strftime('%Y-%m-%D')

Any suggestions on how to have the created column prior work? Thank you


Solution

  • Use pandas' timedelta method:

    df['prior_date'] = df['start_Date'] - pd.Timedelta(90, "d")