类型错误:不支持的操作数类型 -:"日期时间数组"和"日期时间.日期"



我有一个熊猫列在<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')

最新更新