Skip to content

Commit 914b0f3

Browse files
committed
Merge pull request #7973 from cpcloud/transform-oddity-7972
BUG: fix transform with integers
2 parents ab64d58 + 9b75e50 commit 914b0f3

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/source/v0.15.0.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ Bug Fixes
385385

386386

387387

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

390391

391392

pandas/core/groupby.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,7 +2858,9 @@ def transform(self, func, *args, **kwargs):
28582858

28592859
# a grouped that doesn't preserve the index, remap index based on the grouper
28602860
# and broadcast it
2861-
if not isinstance(obj.index,MultiIndex) and type(result.index) != type(obj.index):
2861+
if ((not isinstance(obj.index,MultiIndex) and
2862+
type(result.index) != type(obj.index)) or
2863+
len(result.index) != len(obj.index)):
28622864
results = obj.values.copy()
28632865
for (name, group), (i, row) in zip(self, result.iterrows()):
28642866
indexer = self._get_index(name)
@@ -2868,7 +2870,7 @@ def transform(self, func, *args, **kwargs):
28682870
# we can merge the result in
28692871
# GH 7383
28702872
names = result.columns
2871-
result = obj.merge(result, how='outer', left_index=True, right_index=True).ix[:,-result.shape[1]:]
2873+
result = obj.merge(result, how='outer', left_index=True, right_index=True).iloc[:,-result.shape[1]:]
28722874
result.columns = names
28732875
return result
28742876

pandas/tests/test_groupby.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,6 @@ def test_groupby_categorical_no_compress(self):
31033103
exp = np.array([1,2,4,np.nan])
31043104
self.assert_numpy_array_equivalent(result, exp)
31053105

3106-
31073106
def test_groupby_first_datetime64(self):
31083107
df = DataFrame([(1, 1351036800000000000), (2, 1351036800000000000)])
31093108
df[1] = df[1].view('M8[ns]')
@@ -4500,6 +4499,20 @@ def test_nsmallest(self):
45004499
[0, 4, 1, 6, 7, 8]]))
45014500
tm.assert_series_equal(r, e)
45024501

4502+
def test_transform_doesnt_clobber_ints(self):
4503+
# GH 7972
4504+
n = 6
4505+
x = np.arange(n)
4506+
df = DataFrame({'a': x // 2, 'b': 2.0 * x, 'c': 3.0 * x})
4507+
df2 = DataFrame({'a': x // 2 * 1.0, 'b': 2.0 * x, 'c': 3.0 * x})
4508+
4509+
gb = df.groupby('a')
4510+
result = gb.transform('mean')
4511+
4512+
gb2 = df2.groupby('a')
4513+
expected = gb2.transform('mean')
4514+
tm.assert_frame_equal(result, expected)
4515+
45034516

45044517
def assert_fp_equal(a, b):
45054518
assert (np.abs(a - b) < 1e-12).all()

0 commit comments

Comments
 (0)