Skip to content

Commit ee43575

Browse files
committed
Merge pull request #6347 from jreback/complex
BUG: Bug in setting complex dtypes via boolean indexing (GH6345)
2 parents 5f17e1a + edba489 commit ee43575

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Bug Fixes
9696
- Bug in ``DataFrame.replace()`` when passing a non- ``bool``
9797
``to_replace`` argument (:issue:`6332`)
9898
- Raise when trying to align on different levels of a multi-index assignment (:issue:`3738`)
99+
- Bug in setting complex dtypes via boolean indexing (:issue:`6345`)
99100

100101
pandas 0.13.1
101102
-------------

pandas/core/internals.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,11 @@ class ComplexBlock(FloatOrComplexBlock):
12211221
is_complex = True
12221222

12231223
def _can_hold_element(self, element):
1224-
return isinstance(element, complex)
1224+
if is_list_like(element):
1225+
element = np.array(element)
1226+
return issubclass(element.dtype.type, (np.floating, np.integer, np.complexfloating))
1227+
return (isinstance(element, (float, int, complex, np.float_, np.int_)) and
1228+
not isinstance(bool, np.bool_))
12251229

12261230
def _try_cast(self, element):
12271231
try:

pandas/tests/test_frame.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8736,6 +8736,13 @@ def create():
87368736
result = df.where(pd.notnull(df),DataFrame(1,index=df.index,columns=df.columns))
87378737
assert_frame_equal(result, expected)
87388738

8739+
def test_where_complex(self):
8740+
# GH 6345
8741+
expected = DataFrame([[1+1j, 2], [np.nan, 4+1j]], columns=['a', 'b'])
8742+
df = DataFrame([[1+1j, 2], [5+1j, 4+1j]], columns=['a', 'b'])
8743+
df[df.abs() >= 5] = np.nan
8744+
assert_frame_equal(df,expected)
8745+
87398746
def test_mask(self):
87408747
df = DataFrame(np.random.randn(5, 3))
87418748
cond = df > 0

0 commit comments

Comments
 (0)