在将可变日期与日期的熊猫数据集进行比较时,我遇到了一个令人沮丧的问题。无论我尝试使用哪种格式选项,我都无法使它们符合要求。大家好,我基本上只需要比较熊猫数据集中的日期和今天的日期+ 6个月。
我的代码:
SourceData_Workbook = R"G:\AR\REPORTS\Automation Files\Credit Risk\test1.xlsx"
SourceInPandas = pd.read_excel(SourceData_Workbook, skiprows=33,header=0,index=False)
# Creating date variable + 6 months
six_months = date.today() + relativedelta(months=+6)
# Formatting sourced data to date format
SourceInPandas['Req.dlv.dt']=SourceInPandas['Req.dlv.dt'].apply(lambda x:datetime.strptime(x,'%d.%m.%Y'))
# Fails on this line
SourceInPandas.loc[(SourceInPandas['Req.dlv.dt']<= six_months) & (SourceInPandas['OpIt'] != "15 Overdue account")& (SourceInPandas['OpIt'] != "16 Prepayment required")& (SourceInPandas['OpIt'] != "17 Approval required"),"OpIt"]="Future delivery"
堆栈跟踪:
TypeError: Invalid comparison between dtype=datetime64[ns] and date
您可以通过以下方式Timestamp
使用Timestamp.floor
并添加6个月DateOffset
:
six_months = pd.Timestamp('today').floor('d') + pd.DateOffset(months=6)
print (six_months)
2021-06-10 00:00:00
SourceInPandas['Req.dlv.dt']=pd.to_datetime(SourceInPandas['Req.dlv.dt'], dayfirst=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句