0

The error I am receiving is : TypeError: 'NoneType' object is not subscriptable

In this Method I am trying to do string matching along two files (test&master). The master file contains correctly spelled product names, while the test file contains misspelling or simply differently spelled versions of those products. I am trying to match those together with the extractBests function to include a cutoff at a certain score. Also if I am printing earlier steps of my output, for example the fhp_new variable it still works.

I think the error is somehow caused by the fact, that some rows to not give any matches that are within the score_cutoff limit, because I don't get the error when I put the limit on say 20. So in theory those rows should just stay empty, but this causes the error I think.

This line of code is causing the error:

for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]

This is the full Code until the line of error

def StringMatch (master, testfile, num_match=3, score_cutoff=95, limit=3):
        master_names = master.iloc[:,3]
        test_names = testfile.iloc[:,0]    
        fhp_new = [process.extractBests(x, master_names, score_cutoff=score_cutoff,limit=limit) for x in test_names]
        lab=" "
        i=1

        while i<=num_match:
            lab = lab + " " + "Match" + str(i)
            i = i+1
        aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())

        d={}
        for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]
        print(d)
Tim
  • 161
  • 7
  • 24
  • @user202729 How would one go about that? I am still fairly new and suck at debugging.. thanks for the input! – Tim May 15 '18 at 13:44
  • I told you to check if any objects are None. If you want to know if something is None, just print it out. What's wrong? ... – user202729 May 15 '18 at 13:45
  • The error is telling you that your `y` is `None`, so it probably is not what you think that it is. Inside the for-loop, try printing the aggregated match: `for y in aggregated_matches["Match" + str(x)]: print(y)`. See why it's `None`. – Bram Vanroy May 15 '18 at 14:31
  • @BramVanroy So the return of this is None for a lot of matches because they dont fit my criteria of a score of atleast 95. But I would still like to display this. The result I want is to have the columns Match 1,2,3 and Display all the matches that match this criteria in there. If no match is found, just leave this row empty or if thats not possible jump over it. I guess the error is created because Python doesnt want to do that, so thats the workaround I was looking for. – Tim May 15 '18 at 14:57

1 Answers1

1

If I understand you correctly, you simply want to check if y is None:

def StringMatch (master, testfile, num_match=3, score_cutoff=95, limit=3):
        master_names = master.iloc[:,3]
        test_names = testfile.iloc[:,0]    
        fhp_new = [process.extractBests(x, master_names, score_cutoff=score_cutoff,limit=limit) for x in test_names]
        lab=" "
        i=1

        while i<=num_match:
            lab = lab + " " + "Match" + str(i)
            i = i+1
        aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())

        d={}
        for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [None if y is None else y[0] for y in aggregated_matches["Match" + str(x)]]
        print(d)
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • Thanks for the suggestion, this has been really helpful, I was able to work around this error, now it tells me that the arrays must all be the same length so I have to find a way to include those Nones into the array without giving me the original error. – Tim May 16 '18 at 11:36
  • @Tim In that case, you can do the check in the list comprehension, i.e. `[None if y is None else y[0] for ...]` See my edit. – Bram Vanroy May 16 '18 at 12:04
  • thank you, meanwhile I used a different solution by using the .fillna() on the dataframe and replace the None with a string, but I will test your solution aswell. Thanks! – Tim May 16 '18 at 12:22
  • tested your solution and it works as intended. Thanks again for your support! – Tim May 16 '18 at 12:32