TypeError: 'NoneType' object is not subscriptable
matsujju opened this issue · 1 comments
I am having issue with the code you shared on Medium article named "A Practitioner's Guide to Natural Language Processing (Part I) — Processing & Understanding Text" where contraction.py file was used in the code.
def expand_contractions(text, contraction_mapping=CONTRACTION_MAP):
contractions_pattern = re.compile('({})'.format('|'.join(contraction_mapping.keys())),
flags=re.IGNORECASE|re.DOTALL)
def expand_match(contraction):
match = contraction.group(0)
first_char = match[0]
expanded_contraction = contraction_mapping.get(match)\
if contraction_mapping.get(match)\
else contraction_mapping.get(match.lower())
expanded_contraction = first_char+expanded_contraction[1:]
return expanded_contraction
expanded_text = contractions_pattern.sub(expand_match, text)
expanded_text = re.sub("'", "", expanded_text)
return expanded_text
and while running the function on the data I am getting the error as mentioned in title.
df['Text_of_quest'] = [expand_contractions(x) for x in df['Text_of_quest'].to_list() if x is not None]
Traceback:
TypeError Traceback (most recent call last)
<ipython-input-23-4358bc968219> in <module>
----> 1 get_ipython().run_cell_magic('timeit', '', "df['Text_of_quest'] = [expand_contractions(x) for x in df['Text_of_quest'].to_list() if x is not None]\n")
~/.conda/envs/project1/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2360 with self.builtin_trap:
2361 args = (magic_arg_s, cell)
-> 2362 result = fn(*args, **kwargs)
2363 return result
2364
<decorator-gen-60> in timeit(self, line, cell, local_ns)
~/.conda/envs/project1/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
185 # but it's overkill for just that one bit of state.
186 def magic_deco(arg):
--> 187 call = lambda f, *a, **k: f(*a, **k)
188
189 if callable(arg):
~/.conda/envs/project1/lib/python3.8/site-packages/IPython/core/magics/execution.py in timeit(self, line, cell, local_ns)
1158 for index in range(0, 10):
1159 number = 10 ** index
-> 1160 time_number = timer.timeit(number)
1161 if time_number >= 0.2:
1162 break
~/.conda/envs/project1/lib/python3.8/site-packages/IPython/core/magics/execution.py in timeit(self, number)
167 gc.disable()
168 try:
--> 169 timing = self.inner(it, self.timer)
170 finally:
171 if gcold:
<magic-timeit> in inner(_it, _timer)
<magic-timeit> in <listcomp>(.0)
<ipython-input-9-90eb3c3afe2e> in expand_contractions(text, contraction_mapping)
12 return expanded_contraction
13
---> 14 expanded_text = contractions_pattern.sub(expand_match, text)
15 expanded_text = re.sub("'", "", expanded_text)
16 return expanded_text
<ipython-input-9-90eb3c3afe2e> in expand_match(contraction)
9 if contraction_mapping.get(match)\
10 else contraction_mapping.get(match.lower())
---> 11 expanded_contraction = first_char+expanded_contraction[1:]
12 return expanded_contraction
13
TypeError: 'NoneType' object is not subscriptable
Why NoneType error even after i checked for None value in my dataframe column?
The error is self-explanatory. You are trying to index None. You can not, because 'NoneType' object is not subscriptable. This means that you tried to do:
None[something]
In general, the error means that you attempted to index an object that doesn't have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.