2

I have searched the whole of Stack Overflow and yet to see a solution to this.

Is there a way to check whether a variable/string is NAType?

Column:

DBRef('sector', 29)
DBRef('sector', 29)
DBRef('sector', 29)
DBRef('sector', 29)
DBRef('sector', 29)
DBRef('sector', 29)
<NA>

Loop:

for q in list:
     b = q.split("'")[2]
     newcol.append(b)


Error:

AttributeError: 'NAType' object has no attribute 'split'

Expected:

, 29)
, 29)
, 29)
, 29)
, 29)
, 29)
<NA>

I want to split everything that is not <NA>, if it is then it should do nothing.

jcoke
  • 1,555
  • 1
  • 13
  • 27

1 Answers1

2

(Just because you have pandas tagged), if your data is in a Series/DataFrame, you can use the str methods to do so, which will handle the missing values:

import pandas as pd
import numpy as np

s = pd.Series(["DBRef('sector', 29)"] * 5 + [np.nan])
print(s.str.split("'").str[-1])

Output:

0    , 29)
1    , 29)
2    , 29)
3    , 29)
4    , 29)
5      NaN
dtype: object

But I suppose if you were working within your loop, you could just add an if check:

import pandas as pd

for q in list:
    if not pd.isna(q):
        b = q.split("'")[2]
        newcol.append(b)

Using if pd.isna() rather than simply if because np.nan is truthy.

Tom
  • 8,310
  • 2
  • 16
  • 36