<h3 dir="auto">Describe the workflow you want to enable</h3>
<p dir="auto">Is it possible to support any aggregation function as <code class="notranslate">strategy</code> in <code class="notranslate">SimpleImputer</code>?<br>
The only thing we will need to check is that function returns one non-null value (aggregating) consistently (not random).</p>
<h3 dir="auto">Describe your proposed solution</h3>
<p dir="auto">Support <code class="notranslate">callable</code> as valid <code class="notranslate">strategy</code></p>
<h3 dir="auto">Describe alternatives you've considered, if relevant</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Additional context</h3>
<p dir="auto">This could be an easier and more elegant solution for simple imputer strategies requests that are appearing from time to time.</p>
<p dir="auto">It seems like <code class="notranslate">Imputer</code> can be easily extended to handle arbitrary axis-wise strategies (at least in the case where the data is dense, or sparse with non-explicit entries to be imputed).</p>
<p dir="auto">The <code class="notranslate">strategy</code> parameter could accept a function (or other callable) that reduces a given masked array along an axis, including <a href="http://docs.scipy.org/doc/scipy/reference/stats.mstats.html" rel="nofollow">scipy.stats.mstats</a> functions. More flexibly, <code class="notranslate">strategy</code> could be a callable that either takes a 2d-array and an <code class="notranslate">axis</code> value and returns a 1d-array, or just takes a 1d-array and returns a scalar.</p>
<p dir="auto">For example, one should be able to use:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from functools import partial
from scipy.stats import mstats
Imputer(strategy=partial(mstats.tmean, limits=(.1, None)))"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">functools</span> <span class="pl-k">import</span> <span class="pl-s1">partial</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">stats</span> <span class="pl-k">import</span> <span class="pl-s1">mstats</span>
<span class="pl-v">Imputer</span>(<span class="pl-s1">strategy</span><span class="pl-c1">=</span><span class="pl-en">partial</span>(<span class="pl-s1">mstats</span>.<span class="pl-s1">tmean</span>, <span class="pl-s1">limits</span><span class="pl-c1">=</span>(<span class="pl-c1">.1</span>, <span class="pl-c1">None</span>)))</pre></div>
<p dir="auto">to impute with a trimmed mean using absolute bounds (NB: <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mstats.tmean.html" rel="nofollow"><code class="notranslate">tmean</code></a> doesn't accept an <code class="notranslate">axis</code> argument), or:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Imputer(strategy=lambda X, axis: np.ma.mean(mstats.trimboth(X, .1, axis=axis),
                                            axis=axis)"><pre class="notranslate"><span class="pl-v">Imputer</span>(<span class="pl-s1">strategy</span><span class="pl-c1">=</span><span class="pl-k">lambda</span> <span class="pl-v">X</span>, <span class="pl-s1">axis</span>: <span class="pl-s1">np</span>.<span class="pl-s1">ma</span>.<span class="pl-en">mean</span>(<span class="pl-s1">mstats</span>.<span class="pl-en">trimboth</span>(<span class="pl-v">X</span>, <span class="pl-c1">.1</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-s1">axis</span>),
                                            <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-s1">axis</span>)</pre></div>
<p dir="auto">for a trimmed mean with quantile bounds.</p>