繁体   English   中英

无法比较日期变量和 pandas dataframe 之间的日期

[英]Cannot compare dates between date variable and pandas dataframe

在将可变日期与 pandas 日期数据集进行比较时,我遇到了一个令人沮丧的问题。 无论我尝试什么格式选项,我都无法让它们保持一致。 请大家帮忙,我基本上只需要将 pandas 数据集中的日期与今天的日期 + 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 

您可以将TimestampTimestamp.floor一起使用,并通过DateOffset添加 6 个月:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM