Skip to content
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

categorical series with null converts ints to float #19214

Closed
cstjean opened this issue Jan 12, 2018 · 3 comments · Fixed by #24494
Closed

categorical series with null converts ints to float #19214

cstjean opened this issue Jan 12, 2018 · 3 comments · Fixed by #24494
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@cstjean
Copy link

cstjean commented Jan 12, 2018

In [4]: import pandas as pd, numpy as np

In [5]: sr = pd.Series([1,2, np.nan], dtype="O").astype("category")
   ...: sr
   ...: 
Out[5]: 
0    1.0
1    2.0
2    NaN
dtype: category
Categories (2, int64): [1, 2]

In [6]: sr[0]
Out[6]: 1.0

Shouldn't it keep them as ints? Interestingly, pd.Series(["a", 1,2, np.nan], dtype="O").astype("category") doesn't do the conversion.

@jreback
Copy link
Contributor

jreback commented Jan 13, 2018

so .loc does this correctly, meaning we defer to the underlying data source for getting the values, rather than do the array conversion (which __getitem__) does. So its buggy, changing this may not be easy though.

In [1]: sr = pd.Series([1,2, np.nan], dtype="O").astype("category")

In [2]: sr
Out[2]: 
0    1.0
1    2.0
2    NaN
dtype: category
Categories (2, int64): [1, 2]

In [3]: sr.loc[0]
Out[3]: 1

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Dtype Conversions Unexpected or buggy dtype conversions Categorical Categorical Data Type Difficulty Intermediate labels Jan 13, 2018
@jreback jreback added this to the Next Major Release milestone Jan 13, 2018
@jreback
Copy link
Contributor

jreback commented Jan 13, 2018

cc @toobaz
cc @jschendel

if you want to wade thru some indexing code!

@jreback jreback added the Bug label Jan 13, 2018
@jreback
Copy link
Contributor

jreback commented Jan 13, 2018

pd.Series(["a", 1,2, np.nan], dtype="O").astype("category")

this is as expected, the objs in the series are actually store (and not as a numpy array).

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 Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants