title: Fill missing values by group using most frequent value
body: <p>I am trying to impute missing values using the most frequent value by a group using the <code>pandas</code> module in Python. After checking some posts on Stack Overflow, I have managed to achieve that:</p>
  <pre><code>import numpy as np
  import pandas as pd
  
  df = pd.DataFrame({&quot;group&quot;: [&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;],
                     &quot;value&quot;: [1, 1, 1, np.nan, 2, np.nan, np.nan]})
  df.groupby(&quot;group&quot;).transform(lambda x: x.fillna(x.mode().iloc[0]))
  </code></pre>
  <p>Running this code will fill with 1 the missing entry for group &quot;A&quot; and with 2 both missing entries for group &quot;B&quot;. However, let's assume that one of the groups contains only missing data (in this case group &quot;B&quot;):</p>
  <pre><code>df1 = pd.DataFrame({&quot;group&quot;: [&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;],
                     &quot;value&quot;: [1, 1, 1, np.nan, np.nan, np.nan, np.nan]})
  df1.groupby(&quot;group&quot;).transform(lambda x: x.fillna(x.mode().iloc[0]))
  </code></pre>
  <p>Running the code above will prompt an <code>IndexError: single positional indexer is out-of-bounds</code>. I would expect the normal behaviour is to keep np.nan because if you run the method <code>mode</code> for just, let's say, group &quot;B&quot; from <code>df1</code>:</p>
  <pre><code>df1[df1.group == &quot;B&quot;].mode()
  </code></pre>
  <p>I will get that NaN is the most common value. How can I avoid this issue?</p>