title: How to handle missing values (NaN) in categorical data when using scikit-learn OneHotEncoder?
body: <p>I have recently started learning python to develop a predictive model for a research project using machine learning methods. I have a large dataset comprised of both numerical and categorical data. The dataset has lots of missing values. I am currently trying to encode the categorical features using OneHotEncoder. When I read about OneHotEncoder, my understanding was that for a missing value (NaN), OneHotEncoder would assign 0s to all the feature's categories, as such:</p>
  
  <pre><code>0     Male 
  1     Female
  2     NaN
  </code></pre>
  
  <p>After applying OneHotEncoder:</p>
  
  <pre><code>0     10 
  1     01
  2     00
  </code></pre>
  
  <p>However, when running the following code:</p>
  
  <pre class="lang-py prettyprint-override"><code>    # Encoding categorical data
      from sklearn.compose import ColumnTransformer
      from sklearn.preprocessing import OneHotEncoder
  
  
      ct = ColumnTransformer([('encoder', OneHotEncoder(handle_unknown='ignore'), [1])],
                             remainder='passthrough')
      obj_df = np.array(ct.fit_transform(obj_df))
      print(obj_df)
  
  </code></pre>
  
  <p>I am getting the error <strong>ValueError: Input contains NaN</strong></p>
  
  <p>So I am guessing my previous understanding of how OneHotEncoder handles missing values is wrong. 
  Is there a way for me to get the functionality described above? I know imputing the missing values before encoding will resolve this issue, but I am reluctant to do this as I am dealing with medical data and fear that imputation may decrease the predictive accuracy of my model. </p>
  
  <p>I found this <a href="https://stackoverflow.com/questions/58222008/nan-giving-valueerror-in-onehotencoder-in-scikit-learn">question</a> that is similar but the answer doesn't offer a detailed enough solution on how to deal with the NaN values.</p>
  
  <p>Let me know what your thoughts are, thanks.</p>