Repository: pandas-dev/pandas

Issue:
unstack() produces unexpected dtypes
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
```



Discussion:

Resolving patch:
diff --git a/pandas/core/reshape.py b/pandas/core/reshape.py
--- a/pandas/core/reshape.py
+++ b/pandas/core/reshape.py
@@ -144,15 +144,23 @@ def get_result(self):
 
     def get_new_values(self):
         values = self.values
+
         # place the values
         length, width = self.full_shape
         stride = values.shape[1]
         result_width = width * stride
+        result_shape = (length, result_width)
 
-        dtype, fill_value = _maybe_promote(values.dtype)
-        new_values = np.empty((length, result_width), dtype=dtype)
-        new_values.fill(fill_value)
-        new_mask = np.zeros((length, result_width), dtype=bool)
+        # if our mask is all True, then we can use our existing dtype
+        if self.mask.all():
+            dtype = values.dtype
+            new_values = np.empty(result_shape, dtype=dtype)
+        else:
+            dtype, fill_value = _maybe_promote(values.dtype)
+            new_values = np.empty(result_shape, dtype=dtype)
+            new_values.fill(fill_value)
+
+        new_mask = np.zeros(result_shape, dtype=bool)
 
         # is there a simpler / faster way of doing this?
         for i in xrange(values.shape[1]):


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -8242,6 +8242,41 @@ def test_unstack_to_series(self):
             data = data.unstack()
         assert_frame_equal(old_data, data)
 
+    def test_unstack_dtypes(self):
+
+        # GH 2929
+        rows = [[1, 1, 3, 4],
+                [1, 2, 3, 4],
+                [2, 1, 3, 4],
+                [2, 2, 3, 4]]
+        
+        df = DataFrame(rows, columns=list('ABCD'))
+        result = df.get_dtype_counts()
+        expected = Series({'int64' : 4})
+        assert_series_equal(result, expected)
+
+        # single dtype
+        df2 = df.set_index(['A','B'])
+        df3 = df2.unstack('B')
+        result = df3.get_dtype_counts()
+        expected = Series({'int64' : 4})
+        assert_series_equal(result, expected)
+
+        # mixed
+        df2 = df.set_index(['A','B'])
+        df2['C'] = 3.
+        df3 = df2.unstack('B')
+        result = df3.get_dtype_counts()
+        expected = Series({'int64' : 2, 'float64' : 2})
+        assert_series_equal(result, expected)
+
+        df2['D'] = 'foo'
+        df3 = df2.unstack('B')
+        result = df3.get_dtype_counts()
+        expected = Series({'float64' : 2, 'object' : 2})
+        assert_series_equal(result, expected)
+
+
     def test_reset_index(self):
         stacked = self.frame.stack()[::2]
         stacked = DataFrame({'foo': stacked, 'bar': stacked})
diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py
--- a/pandas/tests/test_multilevel.py
+++ b/pandas/tests/test_multilevel.py
@@ -1346,7 +1346,7 @@ def test_unstack_group_index_overflow(self):
 
         # test roundtrip
         stacked = result.stack()
-        assert_series_equal(s.astype(np.float64),
+        assert_series_equal(s,
                             stacked.reindex(s.index))
 
         # put it at beginning