Skip to content

Commit a6d7698

Browse files
adneujreback
authored andcommitted
BUG: GH12902 fixed coercion of complex values to float when using gro…
closes #12902 Author: adneu <[email protected]> Closes #12917 from adneu/master and squashes the following commits: cd978ad [adneu] BUG: GH12902 added additional test for completeness 2b37396 [adneu] BUG: GH12902 fixed coercion of complex values to float when using groupby
1 parent 7f678ca commit a6d7698

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Diff for: doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,4 @@ Bug Fixes
325325
- ``pd.read_excel()`` now accepts column names associated with keyword argument ``names``(:issue `12870`)
326326

327327
- Bug in ``fill_value`` is ignored if the argument to a binary operator is a constant (:issue `12723`)
328+
- Bug in ``groupby`` where complex types are coerced to float (:issue:`12902`)

Diff for: pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ def _cython_operation(self, kind, values, how, axis):
17471747
values = _algos.ensure_float64(values)
17481748
elif com.is_integer_dtype(values):
17491749
values = values.astype('int64', copy=False)
1750-
elif is_numeric:
1750+
elif is_numeric and not com.is_complex_dtype(values):
17511751
values = _algos.ensure_float64(values)
17521752
else:
17531753
values = values.astype(object)

Diff for: pandas/tests/test_groupby.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,17 @@ def test_groupby_level_0_nonmulti(self):
24752475
result = a.groupby(level=0).sum()
24762476
self.assertEqual(result.index.name, a.index.name)
24772477

2478+
def test_groupby_complex(self):
2479+
# GH 12902
2480+
a = Series(data=np.arange(4) * (1 + 2j), index=[0, 0, 1, 1])
2481+
expected = Series((1 + 2j, 5 + 10j))
2482+
2483+
result = a.groupby(level=0).sum()
2484+
assert_series_equal(result, expected)
2485+
2486+
result = a.sum(level=0)
2487+
assert_series_equal(result, expected)
2488+
24782489
def test_level_preserve_order(self):
24792490
grouped = self.mframe.groupby(level=0)
24802491
exp_labels = np.array([0, 0, 0, 1, 1, 2, 2, 3, 3, 3])
@@ -3254,7 +3265,7 @@ def test_groupby_list_infer_array_like(self):
32543265
def test_groupby_keys_same_size_as_index(self):
32553266
# GH 11185
32563267
freq = 's'
3257-
index = pd.date_range(start=np.datetime64('2015-09-29T11:34:44-0700'),
3268+
index = pd.date_range(start=pd.Timestamp('2015-09-29T11:34:44-0700'),
32583269
periods=2, freq=freq)
32593270
df = pd.DataFrame([['A', 10], ['B', 15]], columns=[
32603271
'metric', 'values'

0 commit comments

Comments
 (0)