title: pandas.apply expects output shape (Shape of passed values is (x,), indices imply (x,y))
body: <p>So I have this pandas.Dataframe</p>
  
  <pre><code>C1    C2     C3    C4    C5    Start    End    C8
  A     1      -      -     -     1        4     -
  A     2      -      -     -     6        10    -
  A     3      -      -     -     11       14    -
  A     4      -      -     -     15       19    -
  </code></pre>
  
  <p>where - are objects, Start is the initial coordinate and end is the final coordinate for each element.</p>
  
  <p>I defined this function to calculate the union of all the intervals in the table, which in this example should sum to [1,19]-{5} (basically a numpy array with all the included elements).</p>
  
  <pre><code>def coverage(table):
      #return a dataframe with the coverage of each individual peptide in a protein 
      interval = (table.apply(lambda row : range(int(row['Start']),int(row['End'])+1),axis=1))] 
      #if there is only one peptide, return the range between its start and end positions 
      if len(table) == 1: return asarray(range(int(table['Start']),int(table['End'])+1)) 
      #if there are more, unite all the intervals 
      if len(table) &gt; 1: 
              return reduce(union1d,(list(interval)))
  </code></pre>
  
  <p>So I apply that function iteratively to several DataFrames (the first one being A, then B,C, etc.). The thing is that for SOME tables this fails and gives away this error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "At_coverage.py", line 37, in &lt;module&gt;
  covdir[prot] = coverage(data)
  File "At_coverage.py", line 21, in coverage
  interval = (table.apply(lambda row : range(int(row['Start']),int(row['End'])+1),axis=1))
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 3312, in apply
  return self._apply_standard(f, axis, reduce=reduce)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 3417, in _apply_standard
  result = self._constructor(data=results, index=index)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 201, in __init__
  mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 323, in _init_dict
  dtype=dtype)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 4473, in _arrays_to_mgr
  return create_block_manager_from_arrays(arrays, arr_names, axes)
  File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3760, in create_block_manager_from_arrays
  construction_error(len(arrays), arrays[0].shape[1:], axes, e)
  File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3732, in construction_error
  passed,implied))
  ValueError: Shape of passed values is (7,), indices imply (7, 8)
  </code></pre>
  
  <p>The DataFrame with which it failed was the following:</p>
  
  <pre><code>               Protein           Peptide  \
  11106  sp|Q75W54|EBM_ARATH           GJDGFJK   
  11107  sp|Q75W54|EBM_ARATH           GJDGFJK   
  11108  sp|Q75W54|EBM_ARATH  JJDPHJVSTFFDDYKR   
  11109  sp|Q75W54|EBM_ARATH  JJDPHJVSTFFDDYKR   
  11110  sp|Q75W54|EBM_ARATH         JNGEPJFJR   
  11111  sp|Q75W54|EBM_ARATH         JNGEPJFJR   
  11112  sp|Q75W54|EBM_ARATH         JNGEPJFJR   
  
                                          Fraction  Count  \
  11106  AT_indark_IEX_fraction_18a_20150422.uniprot-pr...      2   
  11107  AT_indark_IEX_fraction_21a_20150422.uniprot-pr...      2   
  11108  AT_indark_IEX_fraction_18a_20150422.uniprot-pr...      2   
  11109  AT_indark_IEX_fraction_19a_20150422.uniprot-pr...      1   
  11110  AT_indark_IEX_fraction_19a_20150422.uniprot-pr...      2   
  11111  AT_indark_IEX_fraction_22a_20150422.uniprot-pr...      2   
  11112  AT_indark_IEX_fraction_25a_20150422.uniprot-pr...      2   
  
                                              Sequence  Start  End  Length  
  11106  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    577  584     944  
  11107  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    577  584     944  
  11108  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    210  226     944  
  11109  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    210  226     944  
  11110  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    344  353     944  
  11111  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    344  353     944  
  11112  MAEIGKTVLDFGWIAARSTEVDVNGVQLTTTNPPAISSESRWMEAA...    344  353     944  
  
  [7 rows x 8 columns]
  </code></pre>
  
  <p>To make it work I replaced the third line with</p>
  
  <pre><code>    interval = (table.apply(lambda row : range(int(row['Start']),int(row['End'])+4),axis=1)).apply(lambda row: row[:-3])
  </code></pre>
  
  <p>I noticed that it also works with any other number than +1 (although with some others it crashes at another DataFrame later on the loop.</p>
  
  <p>So this solution is redundant and dumb. MY HYPOTHESIS is that the number of rows in this particular data frame matches some weird parameter (like the number of columns or something like that) which makes Pandas try to simplify something and then it crashes. </p>
  
  <p>I made a simplified version of the program that also works for multiple starts and ends:</p>
  
  <pre><code>def multicov(row):
      intervals = []
      for i in range(len(row['Start'])):
      #print data
              intervals.append((range(int(row['Start'][i]),int(row['End'][i])+1)))
      return reduce(union1d,intervals)
  
  
  dir = {'Start':[[1,7],[14]],
      'End':[[5,10],[18]]}
  
  df = DataFrame(dir,columns=['Start','End'])
  print df
  print df.apply(multicov,axis=1)
  </code></pre>
  
  <p>In this case it gives away the same error</p>
  
  <pre><code>ValueError: Shape of passed values is (2,), indices imply (2, 2)
  </code></pre>
  
  <p>But interestingly, if I return two elements from the function (so that it matches 2,2) it behaves well.</p>
  
  <pre><code>return reduce(union1d,intervals),'foobar'
  
  Start      End
  0  [1, 7]  [5, 10]
  1    [14]     [18]
  
  [2 rows x 2 columns]
  0    ([1, 2, 3, 4, 7, 8, 9, 10], foobar)
  1         ([14, 15, 16, 17, 18], foobar)
  dtype: object
  </code></pre>
  
  <p>And if I specify the output as a list, </p>
  
  <pre><code>return [reduce(union1d,intervals),'foobar']
  </code></pre>
  
  <p>it matches the previous column names to the output!</p>
  
  <pre><code>Start      End
  0  [1, 7]  [5, 10]
  1    [14]     [18]
  
  [2 rows x 2 columns]
                 Start     End
  0  [1, 2, 3, 4, 7, 8, 9]  foobar
  1       [14, 15, 16, 17]  foobar
  
  [2 rows x 2 columns]
  </code></pre>
  
  <p>So I assume that the error has to do with Pandas trying to force some compatibility between my previous DataFrame and the one from the output, but I'm surprised that for most DataFrames it works well!</p>