熊猫-具有日期对象的条件逻辑

时间:2019-04-19 03:06:24

标签: python pandas date

尽管我检查了我的pandas列并返回了dtype: datetime64[ns]的dtype,但似乎无法在条件语句中(比较日期

我得到的错误源于底部的第4行:复制到下面

finaldf.loc[(finaldf['Original_Due_Date' >= 'Completed_Date'),'On_Time_Units'] = 'Order_Qty'

错误:

 ValueError: could not convert string to Timestamp

下面的完整代码(无法发布数据集,因为它是私有的)

if day_of_week !=0:
    finaldf['Completed_Date'] = pd.to_datetime(finaldf['Completed_Date'], format="%m/%d/%Y")
    finaldf['Due_Date'] = pd.to_datetime(finaldf['Due_Date'], format="%m/%d/%y") # making it lower case y made it work
    current_week_flags = (finaldf.Completed_Date >= last_monday) & (finaldf.Completed_Date <= today)
    finaldf.loc[(finaldf['Completed_Date'] >= last_monday) & (finaldf['Completed_Date'] <= today) & (finaldf['Due_Date'] < last_monday), 'Due_Date'] = last_monday
    #appears to be working great as of 4.17
    finaldf = finaldf.merge(origdue, on='Work_Order', how= 'left') #vlookup, puts column on outer right 
    finaldf = finaldf.merge(rcode, on='Work_Order', how= 'left')

    #above was working on 4.17
    test = (finaldf.Due_Date >= last_monday) & (finaldf.Due_Date < today)
    finaldf = finaldf[test]
    #above we filtered for the date range, mind the test is  boolean, that called it back in if the value is true

    finaldf = finaldf[finaldf.WO_Stat.str.contains('Complete', na=False)] #make df only contain complete orders
    #the above appears to work great as of 4.18

    #newcolumns = ['Days_Late', 'New_Days_Late', 'Status', 'Day', 'On_Time/Late', 'Cust_PO_#&_WO']
    #finaldf = finaldf.reindex(columns = newcolumns)
    finaldf = finaldf.assign(Days_Late = "", New_Days_Late="", Status="", Day="", On_Time_or_Late="", Cust_PO_WO="", On_Time_Units="", On_Time_Orders="")
    finaldf = finaldf[['column1,column2,Original_Due_Date,column3']]
    #finaldf['Completed_Date'] = pd.to_datetime(finaldf['Completed_Date'], format="%m/%d/%Y").dt.date()
    #finaldf['Orginal_Due_Date'] = pd.to_datetime(finaldf['Original_Due_Date'], format="%m/%d/%Y").dt.date()
    finaldf.loc[(finaldf['Original_Due_Date']>= 'Completed_Date'),'On_Time_Units'] = 'Order_Qty'
    writer = pd.ExcelWriter('currentweek.xlsx', engine='xlsxwriter')
    finaldf.to_excel(writer, index=False, sheet_name='Sheet1')    
    writer.save()

1 个答案:

答案 0 :(得分:1)

这是什么意思?

finaldf['Original_Due_Date' >= 'Completed_Date'

语法错误,应该是

finaldf['Original_Due_Date'] >= 'Completed_Date'

即使如此,您还是将Timestampfinaldf['Original_Due_Date'])与str'Completed_Date')进行比较,因此会出现错误。我想你的意思是:

finaldf['Original_Due_Date'] >= finaldf['Completed_Date']

PS:如果可行,别忘了投票并接受答案:D。

PPS:编辑更新的问题:

flags = (finaldf['Original_Due_Date'] >= finaldf['Completed_Date'])

finaldf.loc[flags, 'On_Time_Units'] = finaldf.loc[flags, 'Order_Qty']

就是这样,我不再回答其他问题。

finaldf.loc[(finaldf['Original_Due_Date']>= finaldf['Completed_Date']),'On_Time_Units'] = finaldf.Order_Qty

这也有效:)