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