<p dir="auto">We need the equivalent of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="316364303" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/20770" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/20770/hovercard" href="https://github.com/pandas-dev/pandas/pull/20770">#20770</a> for the case of partial indexing (which includes both indexing by lists of partial keys, and indexing by specifying a list for each level). It needs a separate approach because resulting indexers are different (as missing keys are dropped rather than replaced with NaN).</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [2]: mi = pd.MultiIndex.from_product([[1, 2], [3,4]])

In [3]: s = pd.Series(-1, index=mi)

In [4]: s.loc[[1, 3],[3, 4]]
Out[4]: 
1  3   -1
   4   -1
dtype: int64"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">2</span>]: <span class="pl-s1">mi</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_product</span>([[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>], [<span class="pl-c1">3</span>,<span class="pl-c1">4</span>]])

<span class="pl-v">In</span> [<span class="pl-c1">3</span>]: <span class="pl-s1">s</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">mi</span>)

<span class="pl-v">In</span> [<span class="pl-c1">4</span>]: <span class="pl-s1">s</span>.<span class="pl-s1">loc</span>[[<span class="pl-c1">1</span>, <span class="pl-c1">3</span>],[<span class="pl-c1">3</span>, <span class="pl-c1">4</span>]]
<span class="pl-v">Out</span>[<span class="pl-c1">4</span>]: 
<span class="pl-c1">1</span>  <span class="pl-c1">3</span>   <span class="pl-c1">-</span><span class="pl-c1">1</span>
   <span class="pl-c1">4</span>   <span class="pl-c1">-</span><span class="pl-c1">1</span>
<span class="pl-s1">dtype</span>: <span class="pl-s1">int64</span></pre></div>
<p dir="auto">In any case, as for <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="316364303" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/20770" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/20770/hovercard" href="https://github.com/pandas-dev/pandas/pull/20770">#20770</a>, it should be possible to emit the warning without checking a second (or first) time for absent keys.</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: <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pandas-dev/pandas/commit/c4da79b5b322c73d8e61d1cb98ac4ab1e2438b40/hovercard" href="https://github.com/pandas-dev/pandas/commit/c4da79b5b322c73d8e61d1cb98ac4ab1e2438b40"><tt>c4da79b</tt></a><br>
python: 3.5.3.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.9.0-6-amd64<br>
machine: x86_64<br>
processor:<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: it_IT.UTF-8<br>
LOCALE: it_IT.UTF-8</p>
<p dir="auto">pandas: 0.23.0.dev0+824.gc4da79b5b.dirty<br>
pytest: 3.5.0<br>
pip: 9.0.1<br>
setuptools: 39.0.1<br>
Cython: 0.25.2<br>
numpy: 1.14.1<br>
scipy: 0.19.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.2.1<br>
sphinx: 1.5.6<br>
patsy: 0.5.0<br>
dateutil: 2.7.0<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: 1.2.0dev<br>
tables: 3.3.0<br>
numexpr: 2.6.1<br>
feather: 0.3.1<br>
matplotlib: 2.0.0<br>
openpyxl: 2.3.0<br>
xlrd: 1.0.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 0.9.6<br>
lxml: 4.1.1<br>
bs4: 4.5.3<br>
html5lib: 0.999999999<br>
sqlalchemy: 1.0.15<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: 0.2.1</p>
</details>
<p dir="auto">This is similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="63471029" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/9697" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/9697/hovercard" href="https://github.com/pandas-dev/pandas/issues/9697">#9697</a>, which was fixed in 0.16.1. I give a (very) slightly modified example here to show some related behavior which is at least inconsistent and should probably be handled cleanly.</p>
<p dir="auto">It's not entirely clear to me what the desired behavior is in this case; it's possible that transform should not work here at all, since it spits out unexpected values. But at minimum it seems like it should do the same thing no matter how I invoke it below.</p>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
df = pd.DataFrame({'col1':[1,1,2,2], 'col2':[1,2,3,np.nan])
#Let's try grouping on 'col2', which contains a NaN.

# Works and gives arguably reasonable results, with one unpredictable value
df.groupby('col2').transform(sum)['col1']

# Throws an unhelpful error
df.groupby('col2')['col1'].transform(sum)"><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-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'col1'</span>:[<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>], <span class="pl-s">'col2'</span>:[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>])
<span class="pl-c">#Let's try grouping on 'col2', which contains a NaN.</span>

<span class="pl-c"># Works and gives arguably reasonable results, with one unpredictable value</span>
<span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'col2'</span>).<span class="pl-en">transform</span>(<span class="pl-s1">sum</span>)[<span class="pl-s">'col1'</span>]

<span class="pl-c"># Throws an unhelpful error</span>
<span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'col2'</span>)[<span class="pl-s">'col1'</span>].<span class="pl-en">transform</span>(<span class="pl-s1">sum</span>)</pre></div>
<p dir="auto">Error is similar to the one encountered in the previous issue:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-54-2d4b83df6487&gt; in &lt;module&gt;()
----&gt; 1 df.groupby('col2')['col1'].transform(sum)

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in transform(self, func, *args, **kwargs)
   2442         cyfunc = _intercept_cython(func)
   2443         if cyfunc and not args and not kwargs:
-&gt; 2444             return self._transform_fast(cyfunc)
   2445 
   2446         # reg transform

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in _transform_fast(self, func)
   2488             values = self._try_cast(values, self._selected_obj)
   2489 
-&gt; 2490         return self._set_result_index_ordered(Series(values))
   2491 
   2492     def filter(self, func, dropna=True, *args, **kwargs):

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in _set_result_index_ordered(self, result)
    503             result = result.sort_index()
    504 
--&gt; 505         result.index = self.obj.index
    506         return result
    507 

/usr/local/lib/python2.7/dist-packages/pandas/core/generic.pyc in __setattr__(self, name, value)
   2159         try:
   2160             object.__getattribute__(self, name)
-&gt; 2161             return object.__setattr__(self, name, value)
   2162         except AttributeError:
   2163             pass

/usr/local/lib/python2.7/dist-packages/pandas/lib.so in pandas.lib.AxisProperty.__set__ (pandas/lib.c:42548)()

/usr/local/lib/python2.7/dist-packages/pandas/core/series.pyc in _set_axis(self, axis, labels, fastpath)
    273         object.__setattr__(self, '_index', labels)
    274         if not fastpath:
--&gt; 275             self._data.set_axis(axis, labels)
    276 
    277     def _set_subtyp(self, is_all_dates):

/usr/local/lib/python2.7/dist-packages/pandas/core/internals.pyc in set_axis(self, axis, new_labels)
   2217         if new_len != old_len:
   2218             raise ValueError('Length mismatch: Expected axis has %d elements, '
-&gt; 2219                              'new values have %d elements' % (old_len, new_len))
   2220 
   2221         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-54-2d4b83df6487&gt; in &lt;module&gt;()
----&gt; 1 df.groupby('col2')['col1'].transform(sum)

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in transform(self, func, *args, **kwargs)
   2442         cyfunc = _intercept_cython(func)
   2443         if cyfunc and not args and not kwargs:
-&gt; 2444             return self._transform_fast(cyfunc)
   2445 
   2446         # reg transform

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in _transform_fast(self, func)
   2488             values = self._try_cast(values, self._selected_obj)
   2489 
-&gt; 2490         return self._set_result_index_ordered(Series(values))
   2491 
   2492     def filter(self, func, dropna=True, *args, **kwargs):

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in _set_result_index_ordered(self, result)
    503             result = result.sort_index()
    504 
--&gt; 505         result.index = self.obj.index
    506         return result
    507 

/usr/local/lib/python2.7/dist-packages/pandas/core/generic.pyc in __setattr__(self, name, value)
   2159         try:
   2160             object.__getattribute__(self, name)
-&gt; 2161             return object.__setattr__(self, name, value)
   2162         except AttributeError:
   2163             pass

/usr/local/lib/python2.7/dist-packages/pandas/lib.so in pandas.lib.AxisProperty.__set__ (pandas/lib.c:42548)()

/usr/local/lib/python2.7/dist-packages/pandas/core/series.pyc in _set_axis(self, axis, labels, fastpath)
    273         object.__setattr__(self, '_index', labels)
    274         if not fastpath:
--&gt; 275             self._data.set_axis(axis, labels)
    276 
    277     def _set_subtyp(self, is_all_dates):

/usr/local/lib/python2.7/dist-packages/pandas/core/internals.pyc in set_axis(self, axis, new_labels)
   2217         if new_len != old_len:
   2218             raise ValueError('Length mismatch: Expected axis has %d elements, '
-&gt; 2219                              'new values have %d elements' % (old_len, new_len))
   2220 
   2221         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements

</code></pre></div>