Notsoprosql
Notsoprosql

Reputation: 199

Python Re apply/search TypeError: 'NoneType' object is not subscriptable

Can someone explain why I am getting this error and how to fix it? I am attempting to search a title for a year 98-99 and I want to get the first part (98):

example title: CAR EBC 98-99

TypeError: 'NoneType' object is not subscriptable

on the year_min line is where the error is occuring.

import pandas as pd
import re

fileinString = 'a.csv'

df1 = pd.read_csv(fileinString, sep=",")

# split title of df1 into string and year tag min and year tag max
regular_expression = re.compile(r'\d\d-\d\d')

title_string = df1['*Title']


year_min = title_string.apply(lambda x: regular_expression.search(x)[0].split('-')[0])

year_max = df1['*Title'].apply(lambda x: regular_expression.search(x)[0].split('-')[1])

print(year_min)

Close Example of it running but It doesnt work: https://ideone.com/JANVt2

Upvotes: 1

Views: 3779

Answers (2)

Alon Gadot
Alon Gadot

Reputation: 565

The exception you're getting is typical for attempting to use bracket notation to access data from a variable containing None.

x = None
x[0]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

I would say one of your text values in title strings does not contain the pattern you're looking for - for which regular_expression.search(x) returns None.

One way you could go about handling these cases is adding some logic to the function you're applying over the column. Something along the lines of:

def extract_pattern(txt):
   match = regular_expression.search(txt)
   if match is None:
      return 'NOT FOUND'
   return match.groups(0).split('-')[0]
year_min = title_string.apply(extract_pattern)

Upvotes: 4

Kunal Sawant
Kunal Sawant

Reputation: 493

This is due to your df1['*Title'] has a value which doesn't match this pattern

When it finds the pattern in the string retuns something

In [18]: regular_expression = re.compile(r'\d\d-\d\d')

In [19]: regular_expression.search('12-18')

Out[19]: <_sre.SRE_Match object; span=(0, 5), match='12-18'>

Where as when doesn't find it returns None

In [20]: regular_expression.search('1218') ==None
Out[20]: True

and None is not subscriptable i.e. you can't do None[0]

so in the end what you're effectively doing is

In [21]: None[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-4b6604f77809> in <module>
----> 1 None[0]

TypeError: 'NoneType' object is not subscriptable

Upvotes: 1

Related Questions