Skip to content

Commit 1675c2a

Browse files
committed
Merge branch 'iss4902' of https://github.com/alefnula/pandas into alefnula-iss4902
Conflicts: doc/source/release.rst
2 parents b037b0e + 75a916a commit 1675c2a

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ pandas/io/*.json
3838
.vagrant
3939
*.whl
4040
**/wheelhouse/*
41+
42+
.project
43+
.pydevproject

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ Bug Fixes
434434
with thousands != "," (:issue:`4596`)
435435
- Bug in getitem with a duplicate index when using where (:issue:`4879`)
436436
- Fix Type inference code coerces float column into datetime (:issue:`4601`)
437+
- Fixed ``_ensure_numeric`` does not check for complex numbers (:issue:`4902`)
437438

438439

439440
pandas 0.12.0

pandas/core/nanops.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,14 @@ def _ensure_numeric(x):
546546
if isinstance(x, np.ndarray):
547547
if x.dtype == np.object_:
548548
x = x.astype(np.float64)
549-
elif not (com.is_float(x) or com.is_integer(x)):
549+
elif not (com.is_float(x) or com.is_integer(x) or com.is_complex(x)):
550550
try:
551551
x = float(x)
552552
except Exception:
553-
raise TypeError('Could not convert %s to numeric' % str(x))
554-
553+
try:
554+
x = complex(x)
555+
except Exception:
556+
raise TypeError('Could not convert %s to numeric' % str(x))
555557
return x
556558

557559
# NA-friendly array comparisons

pandas/tests/test_common.py

+49
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pandas.core.common as com
1515
import pandas.util.testing as tm
1616
import pandas.core.config as cf
17+
from pandas.core import nanops
1718

1819
_multiprocess_can_split_ = True
1920

@@ -311,6 +312,54 @@ def test_ensure_int32():
311312
assert(result.dtype == np.int32)
312313

313314

315+
class TestEnsureNumeric(unittest.TestCase):
316+
def test_numeric_values(self):
317+
# Test integer
318+
self.assertEqual(nanops._ensure_numeric(1), 1, 'Failed for int')
319+
# Test float
320+
self.assertEqual(nanops._ensure_numeric(1.1), 1.1, 'Failed for float')
321+
# Test complex
322+
self.assertEqual(nanops._ensure_numeric(1 + 2j), 1 + 2j,
323+
'Failed for complex')
324+
325+
def test_ndarray(self):
326+
# Test numeric ndarray
327+
values = np.array([1, 2, 3])
328+
self.assertTrue(np.allclose(nanops._ensure_numeric(values), values),
329+
'Failed for numeric ndarray')
330+
331+
# Test object ndarray
332+
o_values = values.astype(object)
333+
self.assertTrue(np.allclose(nanops._ensure_numeric(o_values), values),
334+
'Failed for object ndarray')
335+
336+
# Test convertible string ndarray
337+
s_values = np.array(['1', '2', '3'], dtype=object)
338+
self.assertTrue(np.allclose(nanops._ensure_numeric(s_values), values),
339+
'Failed for convertible string ndarray')
340+
341+
# Test non-convertible string ndarray
342+
s_values = np.array(['foo', 'bar', 'baz'], dtype=object)
343+
self.assertRaises(ValueError,
344+
lambda: nanops._ensure_numeric(s_values))
345+
346+
def test_convertable_values(self):
347+
self.assertTrue(np.allclose(nanops._ensure_numeric('1'), 1.0),
348+
'Failed for convertible integer string')
349+
self.assertTrue(np.allclose(nanops._ensure_numeric('1.1'), 1.1),
350+
'Failed for convertible float string')
351+
self.assertTrue(np.allclose(nanops._ensure_numeric('1+1j'), 1 + 1j),
352+
'Failed for convertible complex string')
353+
354+
def test_non_convertable_values(self):
355+
self.assertRaises(TypeError,
356+
lambda: nanops._ensure_numeric('foo'))
357+
self.assertRaises(TypeError,
358+
lambda: nanops._ensure_numeric({}))
359+
self.assertRaises(TypeError,
360+
lambda: nanops._ensure_numeric([]))
361+
362+
314363
def test_ensure_platform_int():
315364

316365
# verify that when we create certain types of indices

0 commit comments

Comments
 (0)