0

I'm trying to use dateutil.relativedelta.relativedelta to add an int type pandas Series to a datetime Series.

df.loc[:,'calc_eli_date'] = (
    datetime.datetime(df['pol_eff_date'])
    + relativedelta(years=df['frt_elig_year'])
)

df['pol_eff_date'] is datetime64[ns] type

df['frt_elig_year'] is int

But I get the following error:

TypeError: cannot convert the series to class 'int' 
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
mccambe
  • 11
  • 4

1 Answers1

1

The dateutil.relativedata.relativedata constructor only accepts datetime and integer arguments - not arrays or pandas objects.

Try pd.to_timedelta instead:

df['calc_eli_date'] = (
    df['pol_eff_date'] + pd.to_timedelta(df['frt_elig_year'], unit='Y')
)
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
  • got the same error message using above. pol_eff_date is in datetime format eg. 2006-05-03, frt_elig_year is int eg. 15 – mccambe Jun 03 '20 at 00:30
  • sorry error message is: SyntaxError: unexpected EOF while parsing – mccambe Jun 03 '20 at 00:45
  • sorry - updated my answer to drop the datetime.datetime with the same problem. can you post your actual code? if you have a syntax error that's an unrelated problem that we can't debug unless we see your actual implementation. See this post on creating a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) for some really good tips! – Michael Delgado Jun 03 '20 at 00:52
  • df['calc_eli_date'] = (df['pol_eff_date'] +pd.to_timedelta(df['frst_elig_year'], unit='Y') – mccambe Jun 03 '20 at 01:05
  • File "", line 1 df['calc_eli_date'] = (df['pol_eff_date'] +pd.to_timedelta(df['frst_elig_year'], unit='Y') ^ SyntaxError: unexpected EOF while parsing – mccambe Jun 03 '20 at 01:08
  • you're missing a closing paren – Michael Delgado Jun 03 '20 at 01:08
  • I just realized there is also time how can I select only date? – mccambe Jun 03 '20 at 01:48
  • check out https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DatetimeIndex.round.html – Michael Delgado Jun 03 '20 at 01:59