<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="# Your code here
import pandas as pd
df = pd.DataFrame({'A':[10, 20, 30], 'B': [ 'foo', '3', '4'], 'T': [pd.Timestamp(&quot;12:31:22&quot;)]*3})
def get_B(g):
    return g.iloc[0][['B']]
print df.groupby('A').apply(get_B)

# Observed output:
      B
A      
10  NaN
20  3.0
30  4.0"><pre class="notranslate"><span class="pl-c"># Your code here</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-s">'A'</span>:[<span class="pl-c1">10</span>, <span class="pl-c1">20</span>, <span class="pl-c1">30</span>], <span class="pl-s">'B'</span>: [ <span class="pl-s">'foo'</span>, <span class="pl-s">'3'</span>, <span class="pl-s">'4'</span>], <span class="pl-s">'T'</span>: [<span class="pl-s1">pd</span>.<span class="pl-v">Timestamp</span>(<span class="pl-s">"12:31:22"</span>)]<span class="pl-c1">*</span><span class="pl-c1">3</span>})
<span class="pl-k">def</span> <span class="pl-en">get_B</span>(<span class="pl-s1">g</span>):
    <span class="pl-k">return</span> <span class="pl-s1">g</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>][[<span class="pl-s">'B'</span>]]
<span class="pl-k">print</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'A'</span>).<span class="pl-en">apply</span>(<span class="pl-s1">get_B</span>)

<span class="pl-c"># Observed output:</span>
      <span class="pl-v">B</span>
<span class="pl-v">A</span>      
<span class="pl-c1">10</span>  <span class="pl-v">NaN</span>
<span class="pl-c1">20</span>  <span class="pl-c1">3.0</span>
<span class="pl-c1">30</span>  <span class="pl-c1">4.0</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">groupby.apply() does an unexpected conversion from string to float for column 'B' in the example above.  The bug is triggered only when both of the following happen:</p>
<ol dir="auto">
<li>A column ('B' in the example above) has string values, some of which are parseable as numbers and some which are not.</li>
<li>Another column ('T' in the example above) in the dataframe has timestamps.</li>
</ol>
<h4 dir="auto">Expected Output</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="      B
A      
10  foo
20  3
30  4"><pre class="notranslate"><code class="notranslate">      B
A      
10  foo
20  3
30  4
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
# Paste the output here pd.show_versions() here
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 2.7.13.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 7<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.19.2<br>
nose: 1.3.7<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: 0.25.2<br>
numpy: 1.11.3<br>
scipy: 0.18.1<br>
statsmodels: 0.6.1<br>
xarray: None<br>
IPython: 5.1.0<br>
sphinx: 1.5.1<br>
patsy: 0.4.1<br>
dateutil: 2.6.0<br>
pytz: 2016.10<br>
blosc: None<br>
bottleneck: 1.2.0<br>
tables: 3.2.2<br>
numexpr: 2.6.1<br>
matplotlib: 1.5.1<br>
openpyxl: 2.4.0<br>
xlrd: 1.0.0<br>
xlwt: 1.1.2<br>
xlsxwriter: 0.9.6<br>
lxml: 3.7.2<br>
bs4: 4.5.3<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.4<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8.1<br>
boto: 2.45.0<br>
pandas_datareader: 0.2.1</p>
</details>
<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="import pandas as pd

foo = pd.DataFrame.from_records(
    [ 
      (pd.datetime(2016,1,1), 'red', 'dark', 1, '8'),
      (pd.datetime(2015,1,1), 'green', 'stormy', 2, '9'),
      (pd.datetime(2014,1,1), 'blue', 'bright', 3, '10'),
      (pd.datetime(2013,1,1), 'blue', 'calm', 4, 'potato')
    ],
    columns=['observation', 'color', 'mood', 'intensity', 'score'])

# The type of 'score' changes depending on the types passed through the groupby
print(pd.concat(
    [
        foo.dtypes,
        foo.loc[:,['observation', 'color', 'mood', 'intensity', 'score']].groupby('color').apply(lambda g: g.iloc[0]).dtypes,
        foo.loc[:,[               'color', 'mood', 'intensity', 'score']].groupby('color').apply(lambda g: g.iloc[0]).dtypes
    ],
    axis=1,
    keys=['original DF', 'w/ datetime', 'w/o datetime']))"><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-s1">foo</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>.<span class="pl-en">from_records</span>(
    [ 
      (<span class="pl-s1">pd</span>.<span class="pl-en">datetime</span>(<span class="pl-c1">2016</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>), <span class="pl-s">'red'</span>, <span class="pl-s">'dark'</span>, <span class="pl-c1">1</span>, <span class="pl-s">'8'</span>),
      (<span class="pl-s1">pd</span>.<span class="pl-en">datetime</span>(<span class="pl-c1">2015</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>), <span class="pl-s">'green'</span>, <span class="pl-s">'stormy'</span>, <span class="pl-c1">2</span>, <span class="pl-s">'9'</span>),
      (<span class="pl-s1">pd</span>.<span class="pl-en">datetime</span>(<span class="pl-c1">2014</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>), <span class="pl-s">'blue'</span>, <span class="pl-s">'bright'</span>, <span class="pl-c1">3</span>, <span class="pl-s">'10'</span>),
      (<span class="pl-s1">pd</span>.<span class="pl-en">datetime</span>(<span class="pl-c1">2013</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>), <span class="pl-s">'blue'</span>, <span class="pl-s">'calm'</span>, <span class="pl-c1">4</span>, <span class="pl-s">'potato'</span>)
    ],
    <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'observation'</span>, <span class="pl-s">'color'</span>, <span class="pl-s">'mood'</span>, <span class="pl-s">'intensity'</span>, <span class="pl-s">'score'</span>])

<span class="pl-c"># The type of 'score' changes depending on the types passed through the groupby</span>
<span class="pl-en">print</span>(<span class="pl-s1">pd</span>.<span class="pl-en">concat</span>(
    [
        <span class="pl-s1">foo</span>.<span class="pl-s1">dtypes</span>,
        <span class="pl-s1">foo</span>.<span class="pl-s1">loc</span>[:,[<span class="pl-s">'observation'</span>, <span class="pl-s">'color'</span>, <span class="pl-s">'mood'</span>, <span class="pl-s">'intensity'</span>, <span class="pl-s">'score'</span>]].<span class="pl-en">groupby</span>(<span class="pl-s">'color'</span>).<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">g</span>: <span class="pl-s1">g</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>]).<span class="pl-s1">dtypes</span>,
        <span class="pl-s1">foo</span>.<span class="pl-s1">loc</span>[:,[               <span class="pl-s">'color'</span>, <span class="pl-s">'mood'</span>, <span class="pl-s">'intensity'</span>, <span class="pl-s">'score'</span>]].<span class="pl-en">groupby</span>(<span class="pl-s">'color'</span>).<span class="pl-en">apply</span>(<span class="pl-k">lambda</span> <span class="pl-s1">g</span>: <span class="pl-s1">g</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>]).<span class="pl-s1">dtypes</span>
    ],
    <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>,
    <span class="pl-s1">keys</span><span class="pl-c1">=</span>[<span class="pl-s">'original DF'</span>, <span class="pl-s">'w/ datetime'</span>, <span class="pl-s">'w/o datetime'</span>]))</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">When the results of a groupby contain a Series with a datetime and are aggregated back into a DataFrame, columns of object type are cast numeric when possible.  When that Series contains no datetime, they are not.</p>
<p dir="auto">The presence of a datetime elsewhere in the Series should not have effects on unrelated columns.  Doing no implicit type coercion seems (to me) like the safest option (especially in a language where "1" != 1).  But regardless, whether or not type coercion is done for a column 'A' should not depend on the types of all the column 'B's.</p>
<p dir="auto">Issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="182959741" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/14423" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/14423/hovercard" href="https://github.com/pandas-dev/pandas/issues/14423">#14423</a> is a different problem over the same code.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">Current:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="                original DF     w/ datetime w/o datetime
color                object          object       object
intensity             int64           int64        int64
mood                 object          object       object
observation  datetime64[ns]  datetime64[ns]          NaN
score                object           int64       object"><pre class="notranslate"><code class="notranslate">                original DF     w/ datetime w/o datetime
color                object          object       object
intensity             int64           int64        int64
mood                 object          object       object
observation  datetime64[ns]  datetime64[ns]          NaN
score                object           int64       object
</code></pre></div>
<p dir="auto">Expected:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="                original DF     w/ datetime w/o datetime
color                object          object       object
intensity             int64           int64        int64
mood                 object          object       object
observation  datetime64[ns]  datetime64[ns]          NaN
score                object          object       object"><pre class="notranslate"><code class="notranslate">                original DF     w/ datetime w/o datetime
color                object          object       object
intensity             int64           int64        int64
mood                 object          object       object
observation  datetime64[ns]  datetime64[ns]          NaN
score                object          object       object
</code></pre></div>
<p dir="auto"><strong>-or-</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="                original DF     w/ datetime w/o datetime
color                object          object       object
intensity             int64           int64        int64
mood                 object          object       object
observation  datetime64[ns]  datetime64[ns]          NaN
score                object           int64        int64"><pre class="notranslate"><code class="notranslate">                original DF     w/ datetime w/o datetime
color                object          object       object
intensity             int64           int64        int64
mood                 object          object       object
observation  datetime64[ns]  datetime64[ns]          NaN
score                object           int64        int64
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-101-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
<p dir="auto">pandas: 0.19.1<br>
nose: 1.3.1<br>
pip: 1.5.4<br>
setuptools: 3.3<br>
Cython: 0.24.1<br>
numpy: 1.11.2<br>
scipy: 0.18.1<br>
statsmodels: None<br>
xarray: None<br>
IPython: 5.1.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.0<br>
pytz: 2016.10<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: 1.5.3<br>
openpyxl: 2.4.0<br>
xlrd: 1.0.0<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: 3.3.3<br>
bs4: 4.2.1<br>
html5lib: 0.999<br>
httplib2: 0.8<br>
apiclient: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: None<br>
boto: None<br>
pandas_datareader: None</p>
</details>