|
14 | 14 | import pandas.core.common as com
|
15 | 15 | import pandas.util.testing as tm
|
16 | 16 | import pandas.core.config as cf
|
| 17 | +from pandas.core import nanops |
17 | 18 |
|
18 | 19 | _multiprocess_can_split_ = True
|
19 | 20 |
|
@@ -311,6 +312,54 @@ def test_ensure_int32():
|
311 | 312 | assert(result.dtype == np.int32)
|
312 | 313 |
|
313 | 314 |
|
| 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 | + |
314 | 363 | def test_ensure_platform_int():
|
315 | 364 |
|
316 | 365 | # verify that when we create certain types of indices
|
|
0 commit comments