Skip to content

Passing a list to level param of unstack() unexpectedly changes dtype from numeric to object #9913

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
wikiped opened this issue Apr 16, 2015 · 2 comments
Labels
Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@wikiped
Copy link

wikiped commented Apr 16, 2015

I experience these strange behavior of unstack() when its level parameter gets a list instead of string.
This changes dtype of some columns from numeric to object, which is obviously a worrying change.
I am wondering if this a bug or there is something I am missing about dealing with this.
I am on pandas 0.16.0, numpy 1.9.2.

Here is a code to illustrate the problem:

data = """a,b,c,d
2,101,A,11
3,101,A,12
1,201,B,21
2,201,B,22
3,201,B,23
1,301,C,33
2,301,C,32"""
df = pd.read_csv(pd.core.common.StringIO(data))
print df.dtypes
df_str = df.set_index(['a','b']).unstack(level='a')  # Note that level has a str
print df_str.dtypes
df_list = df.set_index(['a','b']).unstack(level=['a'])  # Note that level has a list
print df_list.dtypes

Which produces:

a     int64
b     int64
c    object
d     int64   <-- Note original dtype
dtype: object

   a
c  1     object
   2     object
   3     object
d  1    float64   <-- expected change of dtype due to NaN addition
   2    float64   <-- expected change of dtype due to NaN addition
   3    float64   <-- expected change of dtype due to NaN addition
dtype: object

   a
c  2    object
   3    object
   1    object
d  2    object   <-- UNexpected change of dtype, when level got list
   3    object   <-- UNexpected change of dtype, when level got list
   1    object   <-- UNexpected change of dtype, when level got list
dtype: object
@jreback
Copy link
Contributor

jreback commented Jul 31, 2017

this is fixed in master. if someone can put the appropriate reference, let's close.

@jreback jreback added Dtype Conversions Unexpected or buggy dtype conversions MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jul 31, 2017
@jreback jreback added this to the 0.21.0 milestone Jul 31, 2017
@gfyoung
Copy link
Member

gfyoung commented Jul 31, 2017

@jreback : I believe this is a duplicate #11847 and patched in 0.19.2. Closing as a duplicate.

@wikiped : Could you try upgrading your pandas version? We are currently at 0.20.3.

@gfyoung gfyoung closed this as completed Jul 31, 2017
@gfyoung gfyoung added the Duplicate Report Duplicate issue or pull request label Jul 31, 2017
@gfyoung gfyoung modified the milestones: No action, 0.21.0 Jul 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

3 participants