<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df = pd.DataFrame(data=np.random.randint(0, 5, (5,3)),
                  columns=['a', 'b', 'c'])
&gt;&gt;&gt; df
   a  b  c
0  4  0  0
1  2  0  1
2  2  2  2
3  1  2  2
4  3  0  0

&gt;&gt;&gt; df.apply(lambda x: list(range(2)), axis=1)  # returns a Series
0    [0, 1]
1    [0, 1]
2    [0, 1]
3    [0, 1]
4    [0, 1]
dtype: object

&gt;&gt;&gt; df.apply(lambda x: list(range(3)), axis=1) # returns a DataFrame
   a  b  c
0  0  1  2
1  0  1  2
2  0  1  2
3  0  1  2
4  0  1  2

&gt;&gt;&gt; i = 0
&gt;&gt;&gt; def f(x):
        global i
        if i == 0:
            i += 1
            return list(range(3))
        return list(range(4))

&gt;&gt;&gt; df.apply(f, axis=1) 
ValueError: Shape of passed values is (5, 4), indices imply (5, 3)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">0</span>, <span class="pl-c1">5</span>, (<span class="pl-c1">5</span>,<span class="pl-c1">3</span>)),
                  <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'a'</span>, <span class="pl-s">'b'</span>, <span class="pl-s">'c'</span>])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>
   <span class="pl-s1">a</span>  <span class="pl-s1">b</span>  <span class="pl-s1">c</span>
<span class="pl-c1">0</span>  <span class="pl-c1">4</span>  <span class="pl-c1">0</span>  <span class="pl-c1">0</span>
<span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">0</span>  <span class="pl-c1">1</span>
<span class="pl-c1">2</span>  <span class="pl-c1">2</span>  <span class="pl-c1">2</span>  <span class="pl-c1">2</span>
<span class="pl-c1">3</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">2</span>
<span class="pl-c1">4</span>  <span class="pl-c1">3</span>  <span class="pl-c1">0</span>  <span class="pl-c1">0</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-en">list</span>(<span class="pl-en">range</span>(<span class="pl-c1">2</span>)), <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)  <span class="pl-c"># returns a Series</span>
<span class="pl-c1">0</span>    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>]
<span class="pl-c1">1</span>    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>]
<span class="pl-c1">2</span>    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>]
<span class="pl-c1">3</span>    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>]
<span class="pl-c1">4</span>    [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>]
<span class="pl-s1">dtype</span>: <span class="pl-s1">object</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-en">list</span>(<span class="pl-en">range</span>(<span class="pl-c1">3</span>)), <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>) <span class="pl-c"># returns a DataFrame</span>
   <span class="pl-s1">a</span>  <span class="pl-s1">b</span>  <span class="pl-s1">c</span>
<span class="pl-c1">0</span>  <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>
<span class="pl-c1">1</span>  <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>
<span class="pl-c1">2</span>  <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>
<span class="pl-c1">3</span>  <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>
<span class="pl-c1">4</span>  <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>):
        <span class="pl-k">global</span> <span class="pl-s1">i</span>
        <span class="pl-k">if</span> <span class="pl-s1">i</span> <span class="pl-c1">==</span> <span class="pl-c1">0</span>:
            <span class="pl-s1">i</span> <span class="pl-c1">+=</span> <span class="pl-c1">1</span>
            <span class="pl-k">return</span> <span class="pl-en">list</span>(<span class="pl-en">range</span>(<span class="pl-c1">3</span>))
        <span class="pl-k">return</span> <span class="pl-en">list</span>(<span class="pl-en">range</span>(<span class="pl-c1">4</span>))

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-en">apply</span>(<span class="pl-s1">f</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>) 
<span class="pl-v">ValueError</span>: <span class="pl-v">Shape</span> <span class="pl-s1">of</span> <span class="pl-s1">passed</span> <span class="pl-s1">values</span> <span class="pl-c1">is</span> (<span class="pl-c1">5</span>, <span class="pl-c1">4</span>), <span class="pl-s1">indices</span> <span class="pl-en">imply</span> (<span class="pl-c1">5</span>, <span class="pl-c1">3</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">There are three possible outcomes. When the length of the returned list is equal to the number of columns then a DataFrame is returned and each column gets the corresponding value in the list.</p>
<p dir="auto">If the length of the returned list is not equal to the number of columns, then a Series of lists is returned.</p>
<p dir="auto">If the length of the returned list equals the number of columns for the first row but has at least one row where the list has a different number of elements than number of columns a ValueError is raised.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">Need consistency. Probably should default to a Series of lists for all examples.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.1.final.0<br>
python-bits: 64<br>
OS: Darwin<br>
OS-release: 15.6.0<br>
machine: x86_64<br>
processor: i386<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_US.UTF-8<br>
LOCALE: en_US.UTF-8</p>
<p dir="auto">pandas: 0.21.0rc1<br>
pytest: 3.0.7<br>
pip: 9.0.1<br>
setuptools: 35.0.2<br>
Cython: 0.25.2<br>
numpy: 1.13.3<br>
scipy: 0.19.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.0.0<br>
sphinx: 1.5.5<br>
patsy: 0.4.1<br>
dateutil: 2.6.1<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: 1.2.0<br>
tables: 3.4.2<br>
numexpr: 2.6.2<br>
feather: None<br>
matplotlib: 2.1.0<br>
openpyxl: 2.4.7<br>
xlrd: 1.0.0<br>
xlwt: 1.2.0<br>
xlsxwriter: 0.9.6<br>
lxml: 3.7.3<br>
bs4: 4.6.0<br>
html5lib: 0.9999999<br>
sqlalchemy: 1.1.9<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: 0.5.0</p>
</details>
<h4 dir="auto">MWE</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10, 2))  # dummy array
df1 = df.apply(np.fft.fft, axis=0)  # works
print(df1.shape)  # for testing
df2 = df.apply(np.fft.rfft, axis=0)  # breaks
print(df2.shape)  # for testing"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">rand</span>(<span class="pl-c1">10</span>, <span class="pl-c1">2</span>))  <span class="pl-c"># dummy array</span>
<span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">apply</span>(<span class="pl-s1">np</span>.<span class="pl-s1">fft</span>.<span class="pl-s1">fft</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)  <span class="pl-c"># works</span>
<span class="pl-en">print</span>(<span class="pl-s1">df1</span>.<span class="pl-s1">shape</span>)  <span class="pl-c"># for testing</span>
<span class="pl-s1">df2</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">apply</span>(<span class="pl-s1">np</span>.<span class="pl-s1">fft</span>.<span class="pl-s1">rfft</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)  <span class="pl-c"># breaks</span>
<span class="pl-en">print</span>(<span class="pl-s1">df2</span>.<span class="pl-s1">shape</span>)  <span class="pl-c"># for testing</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">I would like to take a DataFrame of time series and apply the real-fft along the columns, but it seems that DataFrame.apply only works if the function to be applied returns output that is the same size as the input.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I expect the code block above to run without error and produce the following output:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="(10, 2)
(6, 2)"><pre class="notranslate">(<span class="pl-c1">10</span>, <span class="pl-c1">2</span>)
(<span class="pl-c1">6</span>, <span class="pl-c1">2</span>)</pre></div>
<p dir="auto">Alternatively, raising an error that tells the user "output array size must match input array size" would be fine, if we want to restrict apply to working only for functions that return the same-size arrays as the inputs.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.1.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 7<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.20.2<br>
pytest: 3.2.1<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: 0.25.2<br>
numpy: 1.13.1<br>
scipy: 0.19.0<br>
xarray: 0.9.6<br>
IPython: 6.1.0<br>
sphinx: 1.6.2<br>
patsy: None<br>
dateutil: 2.6.0<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: None<br>
tables: 3.2.2<br>
numexpr: 2.6.2<br>
feather: None<br>
matplotlib: 2.0.2<br>
openpyxl: None<br>
xlrd: 1.0.0<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: 0.999<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>