0

I want to add +1 year to each date in my dataframe:

df = pd.read_csv(data_market)

df = df[(df['calendardate'] > '2001-12-31')]
df = df[['ticker','calendardate','marketcap']]

This doesn't work:

df.calendardate = df.calendardate + pd.DateOffset(years=1)

Gives me:

TypeError: must be str, not relativedelta
HelloToEarth
  • 2,027
  • 3
  • 22
  • 48
  • 1
    `df.calendardate += pd.Timedelta(years=1)` – cs95 May 17 '18 at 20:40
  • @cᴏʟᴅsᴘᴇᴇᴅ, that will raise an error, but you could use `days=365` – sacuL May 17 '18 at 20:43
  • @COLDSPEED Raises `ValueError: cannot construct a Timedelta from the passed arguments, allowed keywords are [weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds].` @sacul Raises `TypeError: must be str, not Timedelta` – HelloToEarth May 17 '18 at 20:46
  • 1
    @HelloToEarth, I think that your column is the wrong dtype. Try `df.calendardate = pd.to_datetime(df.calendardate)`, and then either the `Timedelta` solution or your `DateOffset` solution should work. – sacuL May 17 '18 at 20:51
  • Works perfectly now. Thanks! – HelloToEarth May 17 '18 at 21:01

0 Answers0