<p dir="auto">one more trouble case with duplicate column names<br>
the exception could be nicer (sort ambiguous because of duplicate column or similar) or it could sort by the first? column with this name (although this would not be nice in case the column is not duplicate but has in fact different content with the same name and the user not being aware of this).</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd
df = pd.DataFrame([(1,2), (3,4)], columns = ['a', 'a'])
df.sort('a')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-1-251abbb5c77e&gt; in &lt;module&gt;()
      1 import pandas as pd
      2 df = pd.DataFrame([(1,2), (3,4)], columns = ['a', 'a'])
----&gt; 3 df.sort('a')

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in sort(self, columns, column, axis, ascending, inplace)
   3062             columns = column
   3063         return self.sort_index(by=columns, axis=axis, ascending=ascending,
-&gt; 3064                                inplace=inplace)
   3065 
   3066     def sort_index(self, axis=0, by=None, ascending=True, inplace=False):

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in sort_index(self, axis, by, ascending, inplace)
   3125             self._clear_item_cache()
   3126         else:
-&gt; 3127             return self.take(indexer, axis=axis)
   3128 
   3129     def sortlevel(self, level=0, axis=0, ascending=True, inplace=False):

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in take(self, indices, axis)
   2853             new_values = com.take_2d(self.values,
   2854                                      com._ensure_int64(indices),
-&gt; 2855                                      axis=axis)
   2856             if axis == 0:
   2857                 new_columns = self.columns

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/common.pyc in take_2d(arr, indexer, out, mask, needs_masking, axis, fill_value)
    414                 out = np.empty(out_shape, dtype=arr.dtype)
    415             take_f = _get_take2d_function(dtype_str, axis=axis)
--&gt; 416             take_f(arr, _ensure_int64(indexer), out=out, fill_value=fill_value)
    417             return out
    418     elif dtype_str in ('float64', 'object', 'datetime64[ns]'):

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/algos.so in pandas.algos.take_2d_axis0_int64 (pandas/algos.c:73957)()

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)"><pre class="notranslate"><code class="notranslate">import pandas as pd
df = pd.DataFrame([(1,2), (3,4)], columns = ['a', 'a'])
df.sort('a')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-1-251abbb5c77e&gt; in &lt;module&gt;()
      1 import pandas as pd
      2 df = pd.DataFrame([(1,2), (3,4)], columns = ['a', 'a'])
----&gt; 3 df.sort('a')

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in sort(self, columns, column, axis, ascending, inplace)
   3062             columns = column
   3063         return self.sort_index(by=columns, axis=axis, ascending=ascending,
-&gt; 3064                                inplace=inplace)
   3065 
   3066     def sort_index(self, axis=0, by=None, ascending=True, inplace=False):

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in sort_index(self, axis, by, ascending, inplace)
   3125             self._clear_item_cache()
   3126         else:
-&gt; 3127             return self.take(indexer, axis=axis)
   3128 
   3129     def sortlevel(self, level=0, axis=0, ascending=True, inplace=False):

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in take(self, indices, axis)
   2853             new_values = com.take_2d(self.values,
   2854                                      com._ensure_int64(indices),
-&gt; 2855                                      axis=axis)
   2856             if axis == 0:
   2857                 new_columns = self.columns

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/core/common.pyc in take_2d(arr, indexer, out, mask, needs_masking, axis, fill_value)
    414                 out = np.empty(out_shape, dtype=arr.dtype)
    415             take_f = _get_take2d_function(dtype_str, axis=axis)
--&gt; 416             take_f(arr, _ensure_int64(indexer), out=out, fill_value=fill_value)
    417             return out
    418     elif dtype_str in ('float64', 'object', 'datetime64[ns]'):

/usr/local/lib/python2.7/dist-packages/pandas-0.10.0.dev_74ab638-py2.7-linux-x86_64.egg/pandas/algos.so in pandas.algos.take_2d_axis0_int64 (pandas/algos.c:73957)()

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
</code></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="In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'Number' : [1, 2], 'Date' : [&quot;2017-03-02&quot;] * 2, 'Str' : [&quot;foo&quot;, &quot;inf&quot;]})

In [3]: df
Out[3]:
         Date  Number  Str
0  2017-03-02       1  foo
1  2017-03-02       2  inf

In [4]: df.groupby(['Number']).apply(lambda x: x.iloc[0])
Out[4]:
              Date  Number  Str
Number
1       2017-03-02       1  foo
2       2017-03-02       2  inf

In [5]: df.Date = pd.to_datetime(df.Date)

In [6]: df
Out[6]:
        Date  Number  Str
0 2017-03-02       1  foo
1 2017-03-02       2  inf

In [7]: df.groupby(['Number']).apply(lambda x: x.iloc[0])
Out[7]:
             Date  Number  Str
Number
1      2017-03-02       1  NaN
2      2017-03-02       2  inf
"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">1</span>]: <span class="pl-s1">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-v">In</span> [<span class="pl-c1">2</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-s">'Number'</span> : [<span class="pl-c1">1</span>, <span class="pl-c1">2</span>], <span class="pl-s">'Date'</span> : [<span class="pl-s">"2017-03-02"</span>] <span class="pl-c1">*</span> <span class="pl-c1">2</span>, <span class="pl-s">'Str'</span> : [<span class="pl-s">"foo"</span>, <span class="pl-s">"inf"</span>]})

<span class="pl-v">In</span> [<span class="pl-c1">3</span>]: <span class="pl-s1">df</span>
<span class="pl-v">Out</span>[<span class="pl-c1">3</span>]:
         <span class="pl-v">Date</span>  <span class="pl-v">Number</span>  <span class="pl-v">Str</span>
<span class="pl-c1">0</span>  <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">1</span>  <span class="pl-s1">foo</span>
<span class="pl-c1">1</span>  <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">2</span>  <span class="pl-s1">inf</span>

<span class="pl-v">In</span> [<span class="pl-c1">4</span>]: <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>([<span class="pl-s">'Number'</span>]).<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-s1">x</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>])
<span class="pl-v">Out</span>[<span class="pl-c1">4</span>]:
              <span class="pl-v">Date</span>  <span class="pl-v">Number</span>  <span class="pl-v">Str</span>
<span class="pl-v">Number</span>
<span class="pl-c1">1</span>       <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">1</span>  <span class="pl-s1">foo</span>
<span class="pl-c1">2</span>       <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">2</span>  <span class="pl-s1">inf</span>

<span class="pl-v">In</span> [<span class="pl-c1">5</span>]: <span class="pl-s1">df</span>.<span class="pl-v">Date</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">to_datetime</span>(<span class="pl-s1">df</span>.<span class="pl-v">Date</span>)

<span class="pl-v">In</span> [<span class="pl-c1">6</span>]: <span class="pl-s1">df</span>
<span class="pl-v">Out</span>[<span class="pl-c1">6</span>]:
        <span class="pl-v">Date</span>  <span class="pl-v">Number</span>  <span class="pl-v">Str</span>
<span class="pl-c1">0</span> <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">1</span>  <span class="pl-s1">foo</span>
<span class="pl-c1">1</span> <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">2</span>  <span class="pl-s1">inf</span>

<span class="pl-v">In</span> [<span class="pl-c1">7</span>]: <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>([<span class="pl-s">'Number'</span>]).<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-s1">x</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>])
<span class="pl-v">Out</span>[<span class="pl-c1">7</span>]:
             <span class="pl-v">Date</span>  <span class="pl-v">Number</span>  <span class="pl-v">Str</span>
<span class="pl-v">Number</span>
<span class="pl-c1">1</span>      <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">1</span>  <span class="pl-v">NaN</span>
<span class="pl-c1">2</span>      <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">2</span>  <span class="pl-s1">inf</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">When I change the type of the Date column to a Pandas datetime, it causes other columns' types to change in unexpected ways when doing a group-by/apply.  Notice the contents of the "Str" column changes to a numeric type in the final group-by/apply (a contributing factor is probably that one of the elements is the string "inf").  The "inf" value has become inf, and the "foo" value has become NaN.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I expect the Str column to remain a string type, and contain the original strings.  I.e.:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="        Date  Number  Str
0 2017-03-02       1  foo
1 2017-03-02       2  inf"><pre class="notranslate">        <span class="pl-v">Date</span>  <span class="pl-v">Number</span>  <span class="pl-v">Str</span>
<span class="pl-c1">0</span> <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">1</span>  <span class="pl-s1">foo</span>
<span class="pl-c1">1</span> <span class="pl-c1">2017</span><span class="pl-c1">-</span><span class="pl-c1">03</span><span class="pl-c1">-</span><span class="pl-c1">02</span>       <span class="pl-c1">2</span>  <span class="pl-s1">inf</span></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-327.10.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
<p dir="auto">pandas: 0.18.1<br>
nose: 1.3.7<br>
pip: None<br>
setuptools: 0.6<br>
Cython: 0.24.1<br>
numpy: 1.11.1<br>
scipy: 0.18.0<br>
statsmodels: 0.6.1<br>
xarray: 0.7.0<br>
IPython: 5.0.0<br>
sphinx: 1.3.5<br>
patsy: 0.4.1<br>
dateutil: 2.5.3<br>
pytz: 2016.6.1<br>
blosc: None<br>
bottleneck: 1.1.0<br>
tables: 3.2.3.1<br>
numexpr: 2.6.1<br>
matplotlib: 1.5.1<br>
openpyxl: 2.3.2<br>
xlrd: 1.0.0<br>
xlwt: 1.1.2<br>
xlsxwriter: 0.9.2<br>
lxml: 3.6.1<br>
bs4: 4.4.1<br>
html5lib: 0.999<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.0.13<br>
pymysql: 0.6.7.None<br>
psycopg2: 2.5.4 (dt dec pq3 ext)<br>
jinja2: 2.8<br>
boto: 2.40.0<br>
pandas_datareader: None</p>
</details>