【问题标题】:IF-ELSE condition in a function throwing TypeError __floordiv__, ValueError, KeyError函数中的 IF-ELSE 条件抛出 TypeError __floordiv__、ValueError、KeyError
【发布时间】:2021-05-12 19:34:48
【问题描述】:

我需要根据一些条件读取一堆 i/p 数据帧,然后将它们合并,最后创建数据帧为'merge_m0'、'merge_m1'、'merge_m2'等等。

在实际代码中,我需要读取大约 20 个数据帧。但是,为了简单和易于理解,我展示了 3 个数据帧,下面是在“合并”之前运行良好的代码。

但是,对于“合并”之后的 IF-ELSE 条件,它会引发各种错误。我正在使用 globals() 创建 merge_m0,merge_m1,merge_m2....数据帧,但不知道如何处理它。

输入数据帧

df0=pd.DataFrame({'id':[1,2,4],'m0_val':[1,8,10],'name':['A','B','C'],'m0_orig_val':[2,3,4],'m0_int_dt':['07/15/2019','03/21/2019','08/10/2019'],'m0_bpo_dt':['10/15/2019','04/12/2019','06/08/2019']})
df1=pd.DataFrame({'id':[1,2,4],'m1_val':[8,10,1],'name':['D','E','F'],'m1_orig_val':[2,3,4],'m1_int_dt':['02/11/2018','04/06/2019','11/22/2019'],'m1_bpo_dt':['02/15/2018','09/11/2019','12/14/2019']})
df2=pd.DataFrame({'id':[1,2,4],'m2_val':[1,10,8],'name':['G','H','I'],'m2_orig_val':[2,3,4],'m2_int_dt':['10/01/2019','09/03/2018','12/22/2017'],'m2_bpo_dt':['11/15/2018','01/12/2019','08/08/2019']})

代码

import pandas as pd
import datetime

mydate1=datetime.date(2019,12,31)

df_list=[]
for i in range(0,3):
    df_list.append(globals()[f'df{i}']) #appending all the above i/p dataframes to a list

def comb_mths(i):
    dfa = df_list[i]
    dfb = df_list[i+1]
    dfma = dfa[dfa.iloc[:, 1].isin([1,8])] 
    dfmb = dfb[(dfb.iloc[:, 1].isin([8,10])) & (dfb.iloc[:, 3].isin([2,3,4]))]
    globals()[f"merge_m{i}"]  = dfma.merge(dfmb, how='inner', on=['id'])

    #if (globals()[f"merge_m{i}"][f"m{i+1}_orig_val"].isin([2,4])):
    if (globals()[f"merge_m{i}"][f"m{i+1}_orig_val"].isin([2,4])).any():
        globals()[f"merge_m{i}"]['diff'] = mydate1-(globals()[f'merge_m{i}'][f'm{i+1}_int_dt'])//np.timedelta64(1,'M')
        globals()[f'merge_m{i}']['mnths'] = globals()[f"merge_m{i}"]['diff']  - (i-1)

    elif (globals()[f"merge_m{i}"][f"m{i+1}_orig_val"].isin([3,5])).any():
     globals()[f"merge_m{i}"]['diff'] = mydate1-(globals()[f'merge_m{i}'][f'm{i+1}_bpo_dt'])//np.timedelta64(1,'M')
     globals()[f'merge_m{i}']['mnths'] = globals()[f"merge_m{i}"]['diff']  - (i-1)
        
    return globals()[f"merge_m{i}"] 

for i in range(0,2): 
    comb_mths(i)

print(merge_m0)    
print(merge_m1)

IF-ELSE 逻辑

在创建“merge_m{i}”数据框后的上述函数中,我需要检查一个 if-else 条件并计算一个名为“mnths”的变量。 if-else 条件伪代码的逻辑如下:

Ex: when i=0 (1st iteration)
if m1_orig_val isin (2,4):
    diff = (mydate1 - m1_int_dt)//(np.timedelta64(1,'M'))
    mnths = diff - (i-1)
elif m1_orig_val isin (3,5):
    diff = (mydate1 - m1_bpo_dt)//(np.timedelta64(1,'M'))
    mnths = diff - (i-1)

Ex: when i=1 (2nd iteration)

if m2_orig_val isin (2,4):
    diff = (mydate1 - m2_int_dt)//(np.timedelta64(1,'M'))
    mnths = diff - (i-1)
elif m2_orig_val isin (3,5):
    diff = (mydate1 - m2_bpo_dt)//(np.timedelta64(1,'M'))
    mnths = diff - (i-1)

Appreciate if anyone can please show me how do I add this if-else condition in the above function as I'm not sure how to do this using globals() and it's throwing bunch of errors? OR Pls let me know if there is any easy/better alternative to get this done. Thanks!!

编辑

*我做了什么:

更新了上述函数中的 if-elif 行,最初,它在第一个 IF 条件处抛出错误为“ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool() , a.item(), a.any() 或 a.all()"

我在 if 条件中添加了 .any(),但不确定这是否正确,或者我是否需要做其他任何事情

添加 .any() 后,它现在在下一行中抛出一堆错误,我认为如下所述

文件“pandas/_libs/tslibs/timedeltas.pyx”,第 1440 行,在 pandas._libs.tslibs.timedeltas.Timedelta.rfloordiv TypeError:floordiv

的 dtype 对象无效

文件“pandas/_libs/tslibs/timedeltas.pyx”,第 539 行,在 pandas._libs.tslibs.timedeltas.parse_timedelta_unit KeyError: '/'

ValueError:无效的单位缩写:/

如果有人可以帮助我修复这个 if-else 语句,或者如果在上述函数中有任何其他(更好的)方法,请分享。谢谢!

【问题讨论】:

    标签: pandas dataframe if-statement


    【解决方案1】:

    我自己想出了解决办法:

    以下是更新后的功能。在计算“mths”的 If-Else 条件的早期,我使用的是“np.timedelta64”,它引发了各种错误,现在我用不同的逻辑替换了它,并将日期列转换为“日期”类型。现在,该函数运行良好并给出了预期的结果。

    更新代码的解决方案

    import pandas as pd
    import numpy as np
    import datetime
    mydate2=pd.Timestamp('2019-12-31')
    df_list=[]
    for i in range(0,3):
        df_list.append(globals()[f'df{i}'])
    
    def comb_mths(i):
        dfa = df_list[i]
        dfb = df_list[i+1]
        dfa.iloc[:,4]=pd.to_datetime(dfa.iloc[:,4])
        dfa.iloc[:,5]=pd.to_datetime(dfa.iloc[:,5])
        dfb.iloc[:,4]=pd.to_datetime(dfb.iloc[:,4])
        dfb.iloc[:,5]=pd.to_datetime(dfb.iloc[:,5])
        dfma = dfa[dfa.iloc[:, 1].isin([1,8])] 
        dfmb = dfb[(dfb.iloc[:, 1].isin([8,10])) & (dfb.iloc[:, 3].isin([2,3,4]))]
        globals()[f"merge_m{i}"]  = dfma.merge(dfmb, how='inner', on=['id'])
        if (globals()[f"merge_m{i}"][f"m{i+1}_orig_val"].isin([2,4])).any():
            globals()[f"merge_m{i}"]["intnx_dt"]=mydate2-pd.offsets.DateOffset(months=i+1)+pd.offsets.MonthEnd(0)
            globals()[f"merge_m{i}"]['mths']=(globals()[f"merge_m{i}"]["intnx_dt"].dt.year-globals()[f'merge_m{i}'][f'm{i+1}_int_dt'].dt.year)*12+ \
            (globals()[f"merge_m{i}"]["intnx_dt"].dt.month-globals()[f'merge_m{i}'][f'm{i+1}_int_dt'].dt.month)
        elif (globals()[f"merge_m{i}"][f"m{i}_orig_val"].isin([3,5])).any():
            globals()[f"merge_m{i}"]["intnx_dt"]=mydate2-pd.offsets.DateOffset(months=i+1)+pd.offsets.MonthEnd(0)
            globals()[f"merge_m{i}"]['mths']=(globals()[f"merge_m{i}"]["intnx_dt"].dt.year-globals()[f'merge_m{i}'][f'm{i+1}_bpo_dt'].dt.year)*12+\
            (globals()[f"merge_m{i}"]["intnx_dt"].dt.month-globals()[f'merge_m{i}'][f'm{i+1}_bpo_dt'].dt.month)
        return globals()[f"merge_m{i}"]
    
    for i in range(0,2): 
        comb_mths(i)
    

    输出

       id  m0_val name_x  m0_orig_val  m0_int_dt  m0_bpo_dt  m1_val name_y  m1_orig_val  m1_int_dt  m1_bpo_dt   intnx_dt  mths
    0   1       1      A            2 2019-07-15 2019-10-15       8      D            2 2019-07-17 2018-02-15 2019-11-30     4
    1   2       8      B            3 2019-03-21 2019-04-12      10      E            3 2019-03-18 2019-09-11 2019-11-30     8
       id  m1_val name_x  m1_orig_val  m1_int_dt  m1_bpo_dt  m2_val name_y  m2_orig_val  m2_int_dt  m2_bpo_dt   intnx_dt  mths
    0   4       1      F            4 2019-11-22 2019-12-14       8      I            4 2019-05-29 2019-08-08 2019-10-31     5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      相关资源
      最近更新 更多