我有一个熊猫列在<class 'pandas._libs.tslibs.timestamps.Timestamp'>
中,还有一个变量(date_1(在<class 'datetime.date'>
中
我喜欢计算两者之间的月份,如下所示,但得到了错误TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'
df['Duration (months)']= (date_1 - df['Date'])/np.timedelta64(1,'M')
如何计算变量date_1和df["日期"]之间的持续时间(以月为单位(?
尝试to_datetime()
:
date_1=pd.to_datetime(date_1)
或
尝试使用pd.Timestamp()
:
date_1=pd.Timestamp(date_1)
最后:
df['Duration (months)']= (date_1 - df['Date'])/np.timedelta64(1,'M')