title: TypError raised by Pandas module when fitting a Scikit-Learn GridSearchCV
body: <p>Been plagued by this bug for a while now and could use some hive-mind help to (hopefully) catch something I'm missing.</p>
  
  <p><strong>tl;dr version</strong></p>
  
  <p>Pandas is raising a TypeError when I pass scikit-learn GridSearchCV ndarrays of floats to train on.</p>
  
  <p><strong>full version</strong></p>
  
  <p>I'm using an sklearn <a href="http://scikit-learn.org/stable/modules/generated/sklearn.grid_search.GridSearchCV.html" rel="nofollow">GridSearchCV</a> object to fit a 1D list of float target variables scaled between 0.0 and 1.0 to a 2D list of float feature variables (input variables) also scaled between 0 and 1. Both lists have the same number of samples.</p>
  
  <p>The abbreviated code where I pass my training data to GridSearchCV.fit() is as follows:
  </p>
  
  <pre><code># the training data are attributes of a model class defined elsewhere
  model.X_scaled # ndarray of feature data of shape (N_samples, N_features)
  model.Y_scaled # ndarray target data of length N_samples
  
  # setup the GridSearchCV instance
  search = grid_search.GridSearchCV(estimator = svr,
                                            param_grid = params, # C and epsilon parameters are set elsewhere in a dict
                                            n_jobs = self.parallel_processes,
                                            scoring = self.scoring_metric,
                                            cv = np.shape(self.Y)[0], # sets the fold size for cross-val. cv = # of samples is essentially LOO CV.
                                            verbose = 0)
  
  # print out some info to get a better idea of the training data
  print "model.X_scaled"
  print type(model.X_scaled[0][0]) # this is getting the type of one of the elements of X_scaled
  print np.shape(model.X_scaled)
  print model.X_scaled.tolist()
  
  print "model.Y_scaled"
  print type(model.Y_scaled) # this is getting the type of the entire arry structure
  print np.shape(model.Y_scaled)
  print model.Y_scaled
  
  # fit GridSearchCV on the training data
  search.fit(model.X_scaled, model.Y_scaled)
  </code></pre>
  
  <p>When this code is run, I get the resulting output:</p>
  
  <pre><code>model.X_scaled
  &lt;type 'numpy.float64'&gt;
  (81, 16)
  model.Y_scaled
  &lt;type 'numpy.ndarray'&gt;
  (81,)
  Traceback (most recent call last):
    File "SurrogateModel.py", line 416, in &lt;module&gt;
      self_test()
    File "SurrogateModel.py", line 409, in self_test
      model.train()
    File "SurrogateModel.py", line 350, in train
      search.fit(model.X_scaled, model.Y_scaled)
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/grid_search.py", line 804, in fit
      return self._fit(X, y, ParameterGrid(self.param_grid))
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/grid_search.py", line 553, in _fit
      for parameters in parameter_iterable
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 800, in __call__
      while self.dispatch_one_batch(iterator):
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 658, in dispatch_one_batch
      self._dispatch(tasks)
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 566, in _dispatch
      job = ImmediateComputeBatch(batch)
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 180, in __init__
      self.results = batch()
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 72, in __call__
      return [func(*args, **kwargs) for func, args, kwargs in self.items]
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/cross_validation.py", line 1531, in _fit_and_score
      estimator.fit(X_train, y_train, **fit_params)
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/svm/base.py", line 193, in fit
      fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/sklearn/svm/base.py", line 251, in _dense_fit
      max_iter=self.max_iter, random_seed=random_seed)
    File "sklearn/svm/libsvm.pyx", line 59, in sklearn.svm.libsvm.fit (sklearn/svm/libsvm.c:1576)
    File "/home/jack/Software/Python/anaconda2/envs/xs1opt/lib/python2.7/site-packages/pandas/core/series.py", line 78, in wrapper
      "{0}".format(str(converter)))
  TypeError: cannot convert the series to &lt;type 'float'&gt;
  </code></pre>
  
  <p>The stack trace is a bit confusing to me. sklearn makes it all the way to the "fit" call to libsvm, but then a Pandas package raises a TypeError because it can't convert a series object to a float? I looked in the pandas series.py module and found additional context for where the TypeError is being raised:</p>
  
  <pre><code># in pandas/core/series.py
  def _coerce_method(converter):
  """ install the scalar coercion methods """
  
  def wrapper(self):
      if len(self) == 1:
          return converter(self.iloc[0])
      raise TypeError("cannot convert the series to "
                      "{0}".format(str(converter)))
  
  return wrapper
  </code></pre>
  
  <p>I don't understand how this Pandas function is being called when neither of the data structures I'm passing to scikit-learn are pandas objects. In the code I've provided, they're both ndarrays, but I've tried passing them as plain lists only to get the same TypeError. Pandas <strong>is</strong> used earlier in the code to read data from a csv into a DataFrame, but the data is converted to ndarrays before being put into X_scaled and Y_scaled.</p>
  
  <p>The annoying thing is that this very nearly exact same code runs perfectly fine in the script which preceded this one. The version of the code in which I have this problem is basically refactored from the script, but this functionality, training a gridsearch object on the training data, remains mostly unchanged.</p>
  
  <p>Any suggestions on what might be happening here are greatly appreciated. Thank you!</p>