Skip to content

BUG: incorrect broadcasting that could casuse dtype coercion in a groupby-transform #14466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ Bug Fixes
- Bug in string indexing against data with ``object`` ``Index`` may raise ``AttributeError`` (:issue:`14424`)
- Corrrecly raise ``ValueError`` on empty input to ``pd.eval()`` and ``df.query()`` (:issue:`13139`)


- Bug in ``RangeIndex.intersection`` when result is a empty set (:issue:`14364`).
- Bug in union of differences from a ``DatetimeIndex`; this is a regression in 0.19.0 from 0.18.1 (:issue:`14323`)

- Bug in groupby-transform broadcasting that could cause incorrect dtype coercion (:issue:`14457`)


- Bug in ``Series.__setitem__`` which allowed mutating read-only arrays (:issue:`14359`).


Expand Down
21 changes: 15 additions & 6 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3454,7 +3454,6 @@ def _transform_general(self, func, *args, **kwargs):
from pandas.tools.merge import concat

applied = []

obj = self._obj_with_exclusions
gen = self.grouper.get_iterator(obj, axis=self.axis)
fast_path, slow_path = self._define_paths(func, *args, **kwargs)
Expand All @@ -3475,14 +3474,24 @@ def _transform_general(self, func, *args, **kwargs):
else:
res = path(group)

# broadcasting
if isinstance(res, Series):
if res.index.is_(obj.index):
group.T.values[:] = res

# we need to broadcast across the
# other dimension; this will preserve dtypes
# GH14457
if not np.prod(group.shape):
continue
elif res.index.is_(obj.index):
r = concat([res] * len(group.columns), axis=1)
r.columns = group.columns
r.index = group.index
else:
group.values[:] = res
r = DataFrame(
np.concatenate([res.values] * len(group.index)
).reshape(group.shape),
columns=group.columns, index=group.index)

applied.append(group)
applied.append(r)
else:
applied.append(res)

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,18 @@ def nsum(x):
for result in results:
assert_series_equal(result, expected, check_names=False)

def test_transform_coercion(self):

# 14457
# when we are transforming be sure to not coerce
# via assignment
df = pd.DataFrame(dict(A=['a', 'a'], B=[0, 1]))
g = df.groupby('A')

expected = g.transform(np.mean)
result = g.transform(lambda x: np.mean(x))
assert_frame_equal(result, expected)

def test_with_na(self):
index = Index(np.arange(10))

Expand Down