Skip to content

unstack() produces unexpected dtypes #2929

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
heterotopian opened this issue Feb 25, 2013 · 1 comment · Fixed by #3040
Closed

unstack() produces unexpected dtypes #2929

heterotopian opened this issue Feb 25, 2013 · 1 comment · Fixed by #3040
Assignees
Labels
Dtype Conversions Unexpected or buggy dtype conversions
Milestone

Comments

@heterotopian
Copy link

When I unstack() one level of a DataFrame having only int64 column dtypes (and a MultiIndex created only from int64 values), the result will have float64 columns. This happens even when the unstacked DataFrame does not have any missing data.

Is this expected? Is there any way to prevent unstack() from changing the dtypes?

Behavior confirmed with pandas 0.8.1 and 0.10.1.

In [1]: import pandas as pd

In [2]: rows = [[1, 1, 3, 4],
   ...:         [1, 2, 3, 4],
   ...:         [2, 1, 3, 4],
   ...:         [2, 2, 3, 4]]

In [3]: df = pd.DataFrame(rows, columns=list('ABCD'))

In [4]: print df.dtypes
A    int64
B    int64
C    int64
D    int64

In [5]: print df
   A  B  C  D
0  1  1  3  4
1  1  2  3  4
2  2  1  3  4
3  2  2  3  4

In [6]: df2 = df.set_index(['A', 'B'])

In [7]: print df2.dtypes
C    int64
D    int64

In [8]: print df2
     C  D
A B      
1 1  3  4
  2  3  4
2 1  3  4
  2  3  4

In [9]: df3 = df2.unstack('B')

In [10]: print df3.dtypes
   B
C  1    float64
   2    float64
D  1    float64
   2    float64

In [11]: print df3 
   C     D   
B  1  2  1  2
A            
1  3  3  4  4
2  3  3  4  4
@ghost ghost assigned wesm Mar 12, 2013
jreback added a commit that referenced this issue Mar 14, 2013
BUG: Unstack of a frame with no nans would always cause dtype upcasting (GH #2929)
@jreback
Copy link
Contributor

jreback commented Mar 14, 2013

this is fixed, thanks for the report!

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants