Skip to content

BUG: GH12902 fixed coercion of complex values to float when using gro… #12917

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 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,4 @@ Bug Fixes
- ``pd.read_excel()`` now accepts column names associated with keyword argument ``names``(:issue `12870`)

- Bug in ``fill_value`` is ignored if the argument to a binary operator is a constant (:issue `12723`)
- Bug in ``groupby`` where complex types are coerced to float (:issue:`12902`)
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ def _cython_operation(self, kind, values, how, axis):
values = _algos.ensure_float64(values)
elif com.is_integer_dtype(values):
values = values.astype('int64', copy=False)
elif is_numeric:
elif is_numeric and not com.is_complex_dtype(values):
values = _algos.ensure_float64(values)
else:
values = values.astype(object)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,15 @@ def test_groupby_level_0_nonmulti(self):
result = a.groupby(level=0).sum()
self.assertEqual(result.index.name, a.index.name)

def test_groupby_complex(self):
# GH 12902
a = Series(data=np.arange(4) * (1 + 2j), index=[0, 0, 1, 1])
result0 = a.groupby(level=0).sum()
result1 = a.sum(level=0)
expected = Series((1 + 2j, 5 + 10j))
assert_series_equal(result0, expected)
assert_series_equal(result1, expected)

def test_level_preserve_order(self):
grouped = self.mframe.groupby(level=0)
exp_labels = np.array([0, 0, 0, 1, 1, 2, 2, 3, 3, 3])
Expand Down