Home > Enterprise >  TypeError: 'bool' object is not subscriptable with while loop
TypeError: 'bool' object is not subscriptable with while loop

Time:10-22

I want to use while loop to shuffle rows of the 8 dataframes until they are all identical dataframe.

Here are my 8 dataframes:

ITI1=3
ITI2=5
ITI3=7
ItiDurations = list(itertools.repeat(ITI1, 6)) list(itertools.repeat(ITI2,4)) list(itertools.repeat(ITI3,2))
def duplicate(testlist, n):
    return testlist*n
ValCong=['pos', 'neg']
StimValCong = duplicate(ValCong,6)
ActCong=['go', 'nogo']
ActionCong = duplicate(ActCong,6)
# Congruency list (cong=0, incong=1)
Cong = duplicate([0],12)
Conblock_1 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_2 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_3 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_4 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_5 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_6 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_7 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
Conblock_8 = pd.DataFrame(list(zip(ItiDurations,StimValCong,ActionCong,Cong)),columns=['ITI','StimVal', 'Action', 'Congruency'])
dictCon={1:Conblock_1, 2:Conblock_2, 3:Conblock_3, 4:Conblock_4, 5:Conblock_5, 6:Conblock_6, 7:Conblock_7, 8:Conblock_8}

They are identical in the begining.

print(dictCon[1])

    ITI StimVal Action  Congruency
0     3     pos     go           0
1     3     neg   nogo           0
2     3     pos     go           0
3     3     neg   nogo           0
4     3     pos     go           0
5     3     neg   nogo           0
6     5     pos     go           0
7     5     neg   nogo           0
8     5     pos     go           0
9     5     neg   nogo           0
10    7     pos     go           0
11    7     neg   nogo           0

I have first created a function, to check whether I have same dataframes in these 8 dataframes. This function returns the pair keys for the duplicated dataframe in dictionary (dictCon) and boolean when there is duplicates. But returns boolean true, when there is no duplicates.

def CheckBlockOrder(dict):
    li=[1, 2, 3, 4, 5, 6, 7, 8]
    for pair in itertools.combinations(li, 2):
        if dict[pair[0]].equals(dict[pair[1]]):
            return [pair,False]
        else:
            return True

Then I used a while loop to shuffle the rows of the dataframe, if there is a duplicated dataframe:

while CheckBlockOrder(dictCon)[1] is not True:
  pair=CheckBlockOrder(dictCon)[0]
  df_shuffle = dictCon[pair[0]]
  df_shuffle = df_shuffle.iloc[np.random.permutation(df_shuffle.index)].reset_index(drop=True)
  dictCon.update({pair[0]:df_shuffle})

However, running the while loop gives me this error:

    while CheckBlockOrder(dictCon)[1] is not True:
TypeError: 'bool' object is not subscriptable

Interestly, this error happens during the second iteration, not the first one.

I have bebugged for a long time, without a solution. Any suggestions? Thanks!

CodePudding user response:

You are getting this error because CheckBlockOrder(dictCon) returns a boolean in some cases, and you are trying to take the first position of a boolean which you cannot do.

For example:

enter image description here

Quick and dirty fix: instead of returning just True, do the following:

def CheckBlockOrder(dict):
    li=[1, 2, 3, 4, 5, 6, 7, 8]
    for pair in itertools.combinations(li, 2):
        if dict[pair[0]].equals(dict[pair[1]]):
            return [pair,False]
        else:
            return [None,True]
  • Related