8000 Groupby transform preserves output dtype by jcrist · Pull Request #9873 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Groupby transform preserves output dtype #9873

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Groupby transform preserves output dtype
Previously `transform` output was always the same dtype as the groupby
object. This allows the output dtype to differ from the input. Fixes #9807.
  • Loading branch information
jcrist committed Apr 13, 2015
commit b97fd3a8c556a738ab7f2fcc46a884a3cbc8663c
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3006,7 +3006,7 @@ def transform(self, func, *args, **kwargs):
if ((not isinstance(obj.index,MultiIndex) and
type(result.index) != type(obj.index)) or
len(result.index) != len(obj.index)):
results = obj.values.copy()
results = np.empty_like(obj.values, result.values.dtype)
indices = self.indices
for (name, group), (i, row) in zip(self, result.iterrows()):
indexer = indices[name]
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,14 @@ def test_transform_broadcast(self):
for idx in gp.index:
assert_fp_equal(res.xs(idx), agged[idx])

def test_transform_dtype(self):
# GH 9807
# Check transform dtype output is preserved
df = DataFrame([[1, 3], [2, 3]])
result = df.groupby(1).transform('mean')
expected = DataFrame([[1.5], [1.5]])
assert_frame_equal(result, expected)

def test_transform_bug(self):
# GH 5712
# transforming on a datetime column
Expand Down
0