Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ Bug Fixes




- Bug in ``GroupBy.transform()`` where int groups with a transform that
didn't preserve the index were incorrectly truncated (:issue:`7972`).



Expand Down
6 changes: 4 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
Expand Down Expand Up @@ -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()
Expand Down