I'm trying entity extraction with spaCy and Pandas UDF (PySpark) but I get an error.  

Using a UDF works without errors but is slow. What am I doing wrong?

Loading the model every time is to avoid load error -  `Can't find model 'en_core_web_lg'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.`

**Working UDF:**

```
def __get_entities(x):

    global nlp
    nlp = spacy.load("en_core_web_lg")
    ents=[]

    doc = nlp(x)

    for ent in doc.ents:
        if ent.label_ == 'PERSON' OR ent.label_ == 'ORG':
            ents.append(ent.label_)

    return ents

get_entities_udf = F.udf(__get_entities), T.ArrayType(T.StringType()))

```

**Pandas UDF with error:**

```
def __get_entities(x):

    global nlp
    nlp = spacy.load("en_core_web_lg")
    ents=[]

    doc = nlp(x)

    for ent in doc.ents:
        if ent.label_ == 'PERSON' OR ent.label_ == 'ORG':
            ents.append(ent.label_)

    return pd.Series(ents)

get_entities_udf = F.pandas_udf(lambda x: __get_entities(x), "array<string>", F.PandasUDFType.SCALAR)

```

**Error message:**

```
TypeError: Argument 'string'has incorrect type (expected str, got series)

```

**Sample Spark DataFrame:**

```
df = spark.createDataFrame([
  ['John Doe'],
  ['Jane Doe'],
  ['Microsoft Corporation'],
  ['Apple Inc.'],
]).toDF("name",)

```

**New column:**

```
df_new = df.withColumn('entity',get_entities_udf('name'))

```