Skip to content

unstack() produces unexpected dtypes #2929

Closed
@heterotopian

Description

@heterotopian

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

Metadata

Metadata

Assignees

Labels

Dtype ConversionsUnexpected or buggy dtype conversions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions