title: Preserving dtype as category after integer encoding in Pandas DataFrame column
body: <p>I have a Pandas DataFrame read from csv that has some columns with string values but are actually <code>object</code> types. Because they are categorical, I convert them into <code>category</code> and then into integer representation, and then I am fitting a random forest regressor.</p>
  
  <pre><code>for col in df_raw.select_dtypes(include='object'):
      df_raw[col] = df_raw[col].astype('category')
      df_raw[col] = df_raw[col].cat.codes #not 'category' type anymore.
  </code></pre>
  
  <p>The problem is if I do this, then the <code>dtype</code> is immediately converted to <code>int</code> and I lose the <code>cat</code> information, which I need later. </p>
  
  <p>For example, after the first line in the loop, I can run <code>df_raw[col].cat</code>, and I would get the indexed categories as expected. But once the second line is executed, the column <code>dtype</code> changes to <code>int8</code>, I will get the error: </p>
  
  <blockquote>
    <p>Can only use .cat accessor with a 'category' dtype`</p>
  </blockquote>
  
  <p>which, in a way makes perfect sense, since it's dtype is <code>int8</code>.</p>
  
  <p><strong>Is it possible to preserve the category encoding information in the same DataFrame and at the sametime have integer encodings in place to fit the regressor? How?</strong></p>