我有两个日期列(' dob',' paymentDate'):
{'dob': {0: nan, 1: '10/16/1988', 2: nan, 3: nan, 4: nan},
'paymentDate': {0: '20120501',
1: '20100602',
2: '20110601',
3: '20120501',
4: '20110101'}}
我想在这些之间找到几个月的时间差。我只对完成的几个月感兴趣(基本上是"地板" timedelta在几个月内)。在这种特殊情况下,第二行的结果应为259(21年,7个月和17天= 21 * 12 + 7 = 259个月):
{'difference_in_months': {0: nan, 1: 259, 2: nan, 3: nan, 4: nan}}
我该怎么做?我试过了
from dateutil import relativedelta
df_training_data['difference_in_months'] = relativedelta.relativedelta(df_training_data['paymentDate'], df_training_data['dob']).months
但我正在
ValueError: The truth value of a Series is ambiguous
答案 0 :(得分:2)
您可以使用to_datetime
和to_period
df=df.apply(pd.to_datetime,errors = 'coerce',axis=1)
df.paymentDate.dt.to_period('M')-df.dob.dt.to_period('M')-1
Out[95]:
0 NaT
1 259
2 NaT
3 NaT
4 NaT
dtype: object