<p dir="auto">Unintended behaviour of pandas happens when one tries to create a series applying<br>
tuple (or list) to two columns of a dataframe, one of which consists of timestamps:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np
d = pd.DataFrame({'a': pd.Series(np.random.randn(4)), 
                  'b': ['a', 'list', 'of', 'words'], 
                  'ts': pd.date_range('2016-10-01', periods=4, freq='H')})"><pre class="notranslate"><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-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'a'</span>: <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">4</span>)), 
                  <span class="pl-s">'b'</span>: [<span class="pl-s">'a'</span>, <span class="pl-s">'list'</span>, <span class="pl-s">'of'</span>, <span class="pl-s">'words'</span>], 
                  <span class="pl-s">'ts'</span>: <span class="pl-s1">pd</span>.<span class="pl-en">date_range</span>(<span class="pl-s">'2016-10-01'</span>, <span class="pl-s1">periods</span><span class="pl-c1">=</span><span class="pl-c1">4</span>, <span class="pl-s1">freq</span><span class="pl-c1">=</span><span class="pl-s">'H'</span>)})</pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="d"><pre class="notranslate"><span class="pl-s1">d</span></pre></div>
<div dir="auto">
<table role="table">
  <thead>
    <tr>
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>ts</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.200813</td>
      <td>a</td>
      <td>2016-10-01 00:00:00</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.316971</td>
      <td>list</td>
      <td>2016-10-01 01:00:00</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.186392</td>
      <td>of</td>
      <td>2016-10-01 02:00:00</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.565593</td>
      <td>words</td>
      <td>2016-10-01 03:00:00</td>
    </tr>
  </tbody>
</table>
</div>
<p dir="auto">let's try first with columns 'a'and 'b':</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="d[['a', 'b']].apply(tuple, axis=1)"><pre class="notranslate"><span class="pl-s1">d</span>[[<span class="pl-s">'a'</span>, <span class="pl-s">'b'</span>]].<span class="pl-en">apply</span>(<span class="pl-s1">tuple</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="0         (0.2008128669491346, a)
1      (0.3169711841447721, list)
2       (-0.1863916899789735, of)
3    (-0.5655926199699992, words)
dtype: object"><pre class="notranslate"><code class="notranslate">0         (0.2008128669491346, a)
1      (0.3169711841447721, list)
2       (-0.1863916899789735, of)
3    (-0.5655926199699992, words)
dtype: object
</code></pre></div>
<p dir="auto">So far, everything is fine. Now let's do it with 'a' and 'ts':</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="d[['a', 'ts']].apply(tuple, axis=1)"><pre class="notranslate"><span class="pl-s1">d</span>[[<span class="pl-s">'a'</span>, <span class="pl-s">'ts'</span>]].<span class="pl-en">apply</span>(<span class="pl-s1">tuple</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)</pre></div>
<div dir="auto">
<table role="table">
  <thead>
    <tr>
      <th></th>
      <th>a</th>
      <th>ts</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.200813</td>
      <td>2016-10-01 00:00:00</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.316971</td>
      <td>2016-10-01 01:00:00</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.186392</td>
      <td>2016-10-01 02:00:00</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.565593</td>
      <td>2016-10-01 03:00:00</td>
    </tr>
  </tbody>
</table>
</div>
<p dir="auto">Oops.</p>
<p dir="auto">It's easy to find a way around this, by coating the timestamps before apply and uncoating after:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def coating(t):
    return lambda: t

def uncoating(x, f):
    return x, f()"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">coating</span>(<span class="pl-s1">t</span>):
    <span class="pl-k">return</span> <span class="pl-k">lambda</span>: <span class="pl-s1">t</span>

<span class="pl-k">def</span> <span class="pl-en">uncoating</span>(<span class="pl-s1">x</span>, <span class="pl-s1">f</span>):
    <span class="pl-k">return</span> <span class="pl-s1">x</span>, <span class="pl-en">f</span>()</pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="d['coated_ts'] = d['ts'].apply(coating)"><pre class="notranslate"><span class="pl-s1">d</span>[<span class="pl-s">'coated_ts'</span>] <span class="pl-c1">=</span> <span class="pl-s1">d</span>[<span class="pl-s">'ts'</span>].<span class="pl-en">apply</span>(<span class="pl-s1">coating</span>)</pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="d[['a', 'coated_ts']].apply(tuple, axis=1).apply(lambda t: uncoating(*t))"><pre class="notranslate"><span class="pl-s1">d</span>[[<span class="pl-s">'a'</span>, <span class="pl-s">'coated_ts'</span>]].<span class="pl-en">apply</span>(<span class="pl-s1">tuple</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>).<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">t</span>: <span class="pl-en">uncoating</span>(<span class="pl-c1">*</span><span class="pl-s1">t</span>))</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="0     (0.2008128669491346, 2016-10-01 00:00:00)
1     (0.3169711841447721, 2016-10-01 01:00:00)
2    (-0.1863916899789735, 2016-10-01 02:00:00)
3    (-0.5655926199699992, 2016-10-01 03:00:00)
dtype: object"><pre class="notranslate"><code class="notranslate">0     (0.2008128669491346, 2016-10-01 00:00:00)
1     (0.3169711841447721, 2016-10-01 01:00:00)
2    (-0.1863916899789735, 2016-10-01 02:00:00)
3    (-0.5655926199699992, 2016-10-01 03:00:00)
dtype: object
</code></pre></div>
<p dir="auto">It would be nice if this strange behaviour was corrected.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pd.show_versions()"><pre class="notranslate"><span class="pl-s1">pd</span>.<span class="pl-en">show_versions</span>()</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.20.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None"><pre class="notranslate"><code class="notranslate">INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.20.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None
</code></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=""><pre class="notranslate"></pre></div>
<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="d=[['hello',1,'GOOD','long.kw'],
   [1.2,'chipotle',np.nan,'bingo'],
   ['various',np.nan,3000,123.456]]                                                    
t=pd.DataFrame(data=d, columns=['A','B','C','D']) 
t['combined'] = t.apply(lambda x: list([x['A'], x['B'],  x['C'], x['D']]),axis=1)    "><pre class="notranslate"><span class="pl-s1">d</span><span class="pl-c1">=</span>[[<span class="pl-s">'hello'</span>,<span class="pl-c1">1</span>,<span class="pl-s">'GOOD'</span>,<span class="pl-s">'long.kw'</span>],
   [<span class="pl-c1">1.2</span>,<span class="pl-s">'chipotle'</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s">'bingo'</span>],
   [<span class="pl-s">'various'</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">3000</span>,<span class="pl-c1">123.456</span>]]                                                    
<span class="pl-s1">t</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">d</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-s">'D'</span>]) 
<span class="pl-s1">t</span>[<span class="pl-s">'combined'</span>] <span class="pl-c1">=</span> <span class="pl-s1">t</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-s1">x</span>[<span class="pl-s">'A'</span>], <span class="pl-s1">x</span>[<span class="pl-s">'B'</span>],  <span class="pl-s1">x</span>[<span class="pl-s">'C'</span>], <span class="pl-s1">x</span>[<span class="pl-s">'D'</span>]]),<span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)    </pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">[I am confuse why this is not working properly, if I initiate the 'combined' columns first to 0 first, it works. I understand that this is a sub-optimal approach but I am just wondering why is this breaking up]</p>
<h4 dir="auto">Expected Output</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="t['combined'] = t.values.tolist()
t
Out[80]: 
         A         B     C        D                       combined
0    hello         1  GOOD  long.kw      [hello, 1, GOOD, long.kw]
1     1.20  chipotle   NaN    bingo    [1.2, chipotle, nan, bingo]
2  various       NaN  3000   123.46  [various, nan, 3000, 123.456]"><pre class="notranslate"><span class="pl-s1">t</span>[<span class="pl-s">'combined'</span>] <span class="pl-c1">=</span> <span class="pl-s1">t</span>.<span class="pl-s1">values</span>.<span class="pl-en">tolist</span>()
<span class="pl-s1">t</span>
<span class="pl-v">Out</span>[<span class="pl-c1">80</span>]: 
         <span class="pl-v">A</span>         <span class="pl-v">B</span>     <span class="pl-v">C</span>        <span class="pl-v">D</span>                       <span class="pl-s1">combined</span>
<span class="pl-c1">0</span>    <span class="pl-s1">hello</span>         <span class="pl-c1">1</span>  <span class="pl-v">GOOD</span>  <span class="pl-s1">long</span>.<span class="pl-s1">kw</span>      [<span class="pl-s1">hello</span>, <span class="pl-c1">1</span>, <span class="pl-v">GOOD</span>, <span class="pl-s1">long</span>.<span class="pl-s1">kw</span>]
<span class="pl-c1">1</span>     <span class="pl-c1">1.20</span>  <span class="pl-s1">chipotle</span>   <span class="pl-v">NaN</span>    <span class="pl-s1">bingo</span>    [<span class="pl-c1">1.2</span>, <span class="pl-s1">chipotle</span>, <span class="pl-s1">nan</span>, <span class="pl-s1">bingo</span>]
<span class="pl-c1">2</span>  <span class="pl-s1">various</span>       <span class="pl-v">NaN</span>  <span class="pl-c1">3000</span>   <span class="pl-c1">123.46</span>  [<span class="pl-s1">various</span>, <span class="pl-s1">nan</span>, <span class="pl-c1">3000</span>, <span class="pl-c1">123.456</span>]</pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.11.3
scipy: 0.18.1
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.5.1
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.0
tables: 3.2.2
numexpr: 2.6.1
matplotlib: 2.0.0
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.2
bs4: 4.5.3
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
boto: 2.45.0
pandas_datareader: None
</details>