Skip to content
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
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
7 changes: 7 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,13 @@ 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])
result = a.groupby(level=0).sum()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for completeness, can you add s.sum(level=0) as well as a test

expected = Series((1 + 2j, 5 + 10j))
assert_series_equal(result, 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