title: knn imputation of categorical variables in python
body: <p>I am trying to implement kNN from the fancyimpute module on a dataset. I was able to implement the code for continuous variables of the datasets using the code below: </p>
  
  <pre><code>knn_impute2=KNN(k=3).complete(train[['LotArea','LotFrontage']]) 
  </code></pre>
  
  <p>It yields the desirable answer as follows: <a href="https://i.stack.imgur.com/yFjKH.png" rel="nofollow noreferrer">This show how the original dataset looks like and how it has changed using knn imputation</a></p>
  
  <p>I tried to implement the same code for categorical datasets and I get error :</p>
  
  <pre><code>could not convert string to float: 'female'
  </code></pre>
  
  <p>Here is the code I used(I am trying to use Imputer):</p>
  
  <pre><code>from sklearn.preprocessing import Imputer
  imp = Imputer(missing_values='NaN', strategy='most_frequent', axis=0)
  imp.fit(df['sex'])
  print(imp.transform(df['sex']))
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Recap, I want to use knn imputation on this dataset to impute the sex columns. Below is the dataset.</p>
  
  <p><a href="https://i.stack.imgur.com/ctftn.png" rel="nofollow noreferrer">The dataset i want to impute using knn imputation with k value 2</a></p>
  
  <p>How can i do that with knnimpute or i need to write my own functions. If yes, can anyone help me. Thnks</p>