title: Using np.vectorize to create a column in a data frame
body: <p>I have a data fram that contains two columns with numbers and a third column with repeating letters. Let's say somthing like this:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('xy'))
  letters = ['A', 'B', 'C', 'D'] * int(len(df.index) / 4)
  df['letters'] = letters
  </code></pre>
  
  <p>I want to create two new columns, which compares the number in columns 'x' and 'y' to the average of their corresponding letters. For example one new column will just contain the number 10 (if 20% or better than the mean), -10 (if 20% worse than the mean) or else 0.</p>
  
  <p>I wrote the function below:</p>
  
  <pre><code>def scoreFunHigh(dataField, mean, diff, multip):
  
      upper = mean * (1 + diff)
      lower = mean * (1 - diff)
  
      if dataField &gt; upper:
          return multip * 1
      elif dataField &lt; lower:
          return multip * (-1)
      else:
          return 0
  </code></pre>
  
  <p>And then created the column as follows:</p>
  
  <pre><code>letterMeanX = df.groupby('letters')['x'].transform(np.nanmean)
  df['letter x score'] = np.vectorize(scoreFunHigh)(df['x'], letterMeanX, 0.2, 10)
  
  letterMeanY = df.groupby('letters')['y'].transform(np.nanmean)
  df['letter y score'] = np.vectorize(scoreFunHigh)(df['y'], letterMeanY, 0.3, 5)
  </code></pre>
  
  <p>This works. However, I am getting the below runtime waring:</p>
  
  <p>C:\Users\ ... \Python\Python38\lib\site-packages\numpy\lib\function_base.py:2167: RuntimeWarning: invalid value encountered in ? (vectorized)
    outputs = ufunc(*inputs)</p>
  
  <p>(Please note, that if I am running the exact same code as above I am not getting the error. My real dataframe is much larger and I am using several functions for different data) </p>
  
  <p>What is the problem here? Is there a better way to set this up?</p>
  
  <p>Thank you very much</p>