1

I am trying to add a new column to my DataFrame and want it to return the difference in months between two dates which are in two other columns.

I've tried several ways so far, including:

  • simply subtracting the dates and then dividing by 360
    df['TimeInJob'] = (df['OrderDate'] - df['HireDate'] / 360)
  • the to_timedelta method and different parameter settings
  • I attempted this but wasn't sure about the variables they used:

for i in df.index: df.at[i, 'diff'] = relativedelta.relativedelta(df.ix[i, 'start'], df.ix[i, 'end'])

This is my latest attempt:

from dateutil.relativedelta import relativedelta

df['MonthsInJob'] = relativedelta(qf['OrderDate'], df['HireDate'])

but get error message: "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." I have no idea where to include truth values

I need the new column to have the difference in months

martineau
  • 119,623
  • 25
  • 170
  • 301
Matan
  • 117
  • 11

1 Answers1

1

I found this (https://stackoverflow.com/a/42822819/10925117) and it worked for the ultimate end I needed: df['nb_months'] = ((df.date2 - df.date1)/np.timedelta64(1, 'M'))

but still curious about how to get the relativedelta method to work here.

Matan
  • 117
  • 11