title: scipy.optimize.minimize executed multiple times on some rows when being applied to a pandas data frame with or without multiprocessing
body: <p>I'm trying to find the optimal point of a function for each row of a data frame using <code>spicy.optimize.minimize</code>'s <code>L-BFGS-B</code> method, I print the optimal point for each iteration and noticed that some rows have been calculated to find optimal point for twice.</p>
  
  <pre><code>from scipy.optimize import minimize
  import numpy as np
  import pandas as pd
  
  data = pd.DataFrame({'a': [1,2,3,4,5,6], 'b': [7,8,9,10,11,12]})
  
  def func(x, row):
      '''
      calculate result of function
      '''
      return (x - row['a'])*(x - row['b'])
  
  def optimize_func(row):
      '''
      get optimal point
      '''
      res = minimize(func, x0=0, args=row, bounds=[(-10, 10)], method='L-BFGS-B')
      print res.x[0]
      row['optimal_x'] = res.x[0]
      return row
  
  data = data.apply(optimize_func, axis=1)
  </code></pre>
  
  <p>The output is as follows:</p>
  
  <pre><code>4.000000039384161
  4.000000039384161
  4.99999996369956
  5.999999857891461
  6.999999889918595
  7.99999992894573
  8.999999928945726
  </code></pre>
  
  <p>You can see that the first row has been calculated for optimal points for twice.</p>
  
  <p>I'm applying the <code>minimize</code> function on each row, could anybody suggest a possible reason for this? Thanks!</p>
  
  <p>In order to save time to find optimal point for an equation calculated from each row of a data frame, I'm using <code>multiprocessing</code> to speed it up. However, after using <code>multiprocessing</code>, optimization on each row will be executed twice, below is a sample code:</p>
  
  <pre><code>from scipy.optimize import minimize
  import numpy as np
  from multiprocessing import Pool
  from contextlib import closing
  import pandas as pd
  
  data = pd.DataFrame({'a': [1,2,3,4,5,6], 'b': [7,8,9,10,11,12]})
  
  def func(x, row):
      '''
      calculate result of function 1
      '''
      return (x - row['a'])*(x - row['b'])
  
  def optimize_func(row):
      res = minimize(func, x0=0, args=row, bounds=[(-10, 10)], method='L-BFGS-B')
      print res.x[0]
      row['optimal_x'] = res.x[0]
      return row
  
  def apply_optimize_func(data):
      '''
      prepare for multiprocessing
      '''
      return data.apply(optimize_func, axis=1)
  
  def get_optimize_x(data):
      '''
      apply multiprocessing
      '''
      df_parts = np.array_split(data, 20)
      with closing(Pool(processes=10)) as pool:
          result_parts = pool.map(apply_optimize_func, df_parts)
      data = pd.concat(result_parts)
      return data
  
  data = get_optimize_x(data)
  </code></pre>
  
  <p>in order to show that the optimization is executed twice for each row, I added a <code>print</code> statement to <code>optimize_func()</code> and the printed optimal points are as follows:</p>
  
  <pre><code>4.000000039384161
  4.000000039384161
  4.99999996369956
  4.99999996369956
  5.999999857891461
  5.999999857891461
  6.999999889918595
  6.999999889918595
  7.99999992894573
  7.99999992894573
  8.999999928945726
  8.999999928945726
  </code></pre>
  
  <p>as you can see, each point is generated twice, I'm wondering why this is happening? Thanks!</p>