<p dir="auto">I find that <code class="notranslate">.last()</code> does not perform as expected.</p>
<h4 dir="auto">Example:</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df = pd.DataFrame([[179293473,'2016-06-01 00:00:03.549745','http://www.dr.dk/nyheder/',39169523],[179293473,'2016-06-01 00:04:22.346018','https://www.information.dk/indland/2016/05/hvert-tredje-offer-naar-anmelde-voldtaegt-tide', 39125224],
 [179773461, '2016-06-01 22:13:16.588146', 'https://www.google.dk', 31658124],
 [179773461, '2016-06-01 22:14:04.059781', 'https://www.google.dk', 31658124],
 [179773461, '2016-06-01 22:16:37.230587', np.nan, 31658124],
 [179773461, '2016-06-01 22:23:09.847149', 'https://www.google.dk', 32718401],
 [179773461, '2016-06-01 22:23:55.158929', np.nan, 32718401],
 [179773461, '2016-06-01 22:27:00.857224', np.nan, 32718401]],
columns=['SessionID', 'PageTime', 'ReferrerURL', 'PageID'])"><pre class="notranslate"><code class="notranslate">df = pd.DataFrame([[179293473,'2016-06-01 00:00:03.549745','http://www.dr.dk/nyheder/',39169523],[179293473,'2016-06-01 00:04:22.346018','https://www.information.dk/indland/2016/05/hvert-tredje-offer-naar-anmelde-voldtaegt-tide', 39125224],
 [179773461, '2016-06-01 22:13:16.588146', 'https://www.google.dk', 31658124],
 [179773461, '2016-06-01 22:14:04.059781', 'https://www.google.dk', 31658124],
 [179773461, '2016-06-01 22:16:37.230587', np.nan, 31658124],
 [179773461, '2016-06-01 22:23:09.847149', 'https://www.google.dk', 32718401],
 [179773461, '2016-06-01 22:23:55.158929', np.nan, 32718401],
 [179773461, '2016-06-01 22:27:00.857224', np.nan, 32718401]],
columns=['SessionID', 'PageTime', 'ReferrerURL', 'PageID'])
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">When I run:<br>
<code class="notranslate">df.groupby('SessionID').last()</code><br>
I get:</p>
<table role="table">
<thead>
<tr>
<th>  SessionID</th>
<th>PageTime</th>
<th>ReferrerURL</th>
<th>PageID</th>
</tr>
</thead>
<tbody>
<tr>
<td>179293473</td>
<td>2016-06-01 00:04:22.346018</td>
<td><a href="https://www.information.dk/indland/2016/05/hve" rel="nofollow">https://www.information.dk/indland/2016/05/hve</a>...</td>
<td>39125224</td>
</tr>
<tr>
<td>179773461</td>
<td>2016-06-01 22:27:00.857224</td>
<td><a href="https://www.google.dk" rel="nofollow">https://www.google.dk</a></td>
<td>32718401</td>
</tr>
</tbody>
</table>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">When, in fact, I was expecting the same result as obtained from:<br>
<code class="notranslate">df.groupby('SessionID').nth(-1)</code></p>
<table role="table">
<thead>
<tr>
<th>  SessionID</th>
<th>PageID</th>
<th>PageTime</th>
<th>ReferrerURL</th>
</tr>
</thead>
<tbody>
<tr>
<td>179293473</td>
<td>39125224</td>
<td>2016-06-01 00:04:22.346018</td>
<td><a href="https://www.information.dk/indland/2016/05/hve" rel="nofollow">https://www.information.dk/indland/2016/05/hve</a>...</td>
</tr>
<tr>
<td>179773461</td>
<td>32718401</td>
<td>2016-06-01 22:27:00.857224</td>
<td>NaN</td>
</tr>
</tbody>
</table>
<hr>
<p dir="auto">And while we are at <code class="notranslate">.nth()</code>, why does it mix up my column order?</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.4.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 10<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 79 Stepping 1, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: 3.3.2<br>
pip: 9.0.3<br>
setuptools: 38.4.0<br>
Cython: 0.27.3<br>
numpy: 1.14.0<br>
scipy: 1.0.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.2.1<br>
sphinx: 1.6.6<br>
patsy: 0.5.0<br>
dateutil: 2.6.1<br>
pytz: 2017.3<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.2<br>
numexpr: 2.6.4<br>
feather: None<br>
matplotlib: 2.1.2<br>
openpyxl: 2.4.10<br>
xlrd: 1.1.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 1.0.2<br>
lxml: 4.1.1<br>
bs4: 4.6.0<br>
html5lib: 1.0.1<br>
sqlalchemy: 1.2.1<br>
pymysql: None<br>
psycopg2: 2.7.4 (dt dec pq3 ext lo64)<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">This is incorrect, as this is applied column by column (as they are different dtypes)<br>
so <code class="notranslate">_first_compat</code> should first compute the mask then use it.</p>
<p dir="auto">from <a href="http://stackoverflow.com/questions/26108181/selecting-first-row-with-groupby-and-nan-columns" rel="nofollow">SO</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [18]: df = pd.DataFrame([{'id':&quot;a&quot;,'val':np.nan, 'val2':-1},{'id':&quot;a&quot;,'val':'TREE','val2':15}])

In [19]: df
Out[19]: 
  id   val  val2
0  a   NaN    -1
1  a  TREE    15

In [20]: df.groupby('id').first()
Out[20]: 
     val  val2
id            
a   TREE    -1"><pre class="notranslate"><code class="notranslate">In [18]: df = pd.DataFrame([{'id':"a",'val':np.nan, 'val2':-1},{'id':"a",'val':'TREE','val2':15}])

In [19]: df
Out[19]: 
  id   val  val2
0  a   NaN    -1
1  a  TREE    15

In [20]: df.groupby('id').first()
Out[20]: 
     val  val2
id            
a   TREE    -1
</code></pre></div>