Skip to content

BUG: remove_unused_categories dtype coerces to int64 #13261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

sinhrks
Copy link
Member

@sinhrks sinhrks commented May 23, 2016

  • tests added / passed
  • passes git diff upstream/master | flake8 --diff
  • whatsnew entry
c = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
c.codes
# array([0, 1], dtype=int8)

# NG, must be int8 dtype
c = c.remove_unused_categories()
c.codes
# array([0, 1])

It is because np.unique uses platform int for unique_inverse

np.unique(np.array([0, 3, 2, 3], dtype=np.int8), return_inverse=True)
(array([0, 2, 3], dtype=int8), array([0, 2, 1, 2]))

@sinhrks sinhrks added Bug Dtype Conversions Unexpected or buggy dtype conversions Categorical Categorical Data Type labels May 23, 2016
@sinhrks sinhrks added this to the 0.18.2 milestone May 23, 2016
@sinhrks sinhrks force-pushed the categorical_remove_dtype branch from b3f817e to f8a36ce Compare May 23, 2016 21:55
@@ -882,6 +882,7 @@ def remove_unused_categories(self, inplace=False):

if idx.size != 0 and idx[0] == -1: # na sentinel
idx, inv = idx[1:], inv - 1
inv = inv.astype(np.int8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't do this, either use pd.unique() or _coerce_indexer_dtype()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add an arg to pd.unique() to return the inverses, which we have (i think directly)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misunderstood that .codes is always np.int8. changed to use_coerce_indexer_dtype().

@sinhrks sinhrks force-pushed the categorical_remove_dtype branch from f8a36ce to b5cbe2c Compare May 23, 2016 22:05
@jreback jreback closed this in 75714de May 24, 2016
@jreback
Copy link
Contributor

jreback commented May 24, 2016

thank you sir!

@sinhrks sinhrks deleted the categorical_remove_dtype branch May 24, 2016 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants