Repository: pandas-dev/pandas

Issue:
BUG: odd transform behaviour with integers
After grouping on an integer column, we seem to forget that we have groups:

```
>>> pd.__version__
'0.14.1-172-gab64d58'
>>> x = np.arange(6, dtype=np.int64)
>>> df = pd.DataFrame({"a": x//2, "b": 2.0*x, "c": 3.0*x})
>>> df
   a   b   c
0  0   0   0
1  0   2   3
2  1   4   6
3  1   6   9
4  2   8  12
5  2  10  15
>>> df.groupby("a").transform("mean")
    b     c
0   1   1.5
1   5   7.5
2   9  13.5
3 NaN   NaN
4 NaN   NaN
5 NaN   NaN
>>> df["a"] = df["a"]*1.0
>>> df.groupby("a").transform("mean")
   b     c
0  1   1.5
1  1   1.5
2  5   7.5
3  5   7.5
4  9  13.5
5  9  13.5
```

To make it even more obvious:

```
>>> df.index = range(20, 26)
>>> df.groupby("a").transform("mean")
     b     c
0    1   1.5
1    5   7.5
2    9  13.5
20 NaN   NaN
21 NaN   NaN
22 NaN   NaN
23 NaN   NaN
24 NaN   NaN
25 NaN   NaN
```

Switching to a float index seems to avoid the issues as well.



Discussion:
I think the `.ix` should be `.iloc` here: https://github.com/pydata/pandas/blob/master/pandas/core/groupby.py#L2871 to fix

can you give a try ?

that doesn't fix it i just tried


Resolving patch:
diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py
--- a/pandas/core/groupby.py
+++ b/pandas/core/groupby.py
@@ -2858,7 +2858,9 @@ def transform(self, func, *args, **kwargs):
 
         # a grouped that doesn't preserve the index, remap index based on the grouper
         # and broadcast it
-        if not isinstance(obj.index,MultiIndex) and type(result.index) != type(obj.index):
+        if ((not isinstance(obj.index,MultiIndex) and
+             type(result.index) != type(obj.index)) or
+            len(result.index) != len(obj.index)):
             results = obj.values.copy()
             for (name, group), (i, row) in zip(self, result.iterrows()):
                 indexer = self._get_index(name)
@@ -2868,7 +2870,7 @@ def transform(self, func, *args, **kwargs):
         # we can merge the result in
         # GH 7383
         names = result.columns
-        result = obj.merge(result, how='outer', left_index=True, right_index=True).ix[:,-result.shape[1]:]
+        result = obj.merge(result, how='outer', left_index=True, right_index=True).iloc[:,-result.shape[1]:]
         result.columns = names
         return result
 


Test patch:
diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py
--- a/pandas/tests/test_groupby.py
+++ b/pandas/tests/test_groupby.py
@@ -3103,7 +3103,6 @@ def test_groupby_categorical_no_compress(self):
         exp = np.array([1,2,4,np.nan])
         self.assert_numpy_array_equivalent(result, exp)
 
-
     def test_groupby_first_datetime64(self):
         df = DataFrame([(1, 1351036800000000000), (2, 1351036800000000000)])
         df[1] = df[1].view('M8[ns]')
@@ -4500,6 +4499,20 @@ def test_nsmallest(self):
                                                  [0, 4, 1, 6, 7, 8]]))
         tm.assert_series_equal(r, e)
 
+    def test_transform_doesnt_clobber_ints(self):
+        # GH 7972
+        n = 6
+        x = np.arange(n)
+        df = DataFrame({'a': x // 2, 'b': 2.0 * x, 'c': 3.0 * x})
+        df2 = DataFrame({'a': x // 2 * 1.0, 'b': 2.0 * x, 'c': 3.0 * x})
+
+        gb = df.groupby('a')
+        result = gb.transform('mean')
+
+        gb2 = df2.groupby('a')
+        expected = gb2.transform('mean')
+        tm.assert_frame_equal(result, expected)
+
 
 def assert_fp_equal(a, b):
     assert (np.abs(a - b) < 1e-12).all()