|
24 | 24 | _multiprocess_can_split_ = True
|
25 | 25 |
|
26 | 26 |
|
27 |
| -def test_downcast_conv(): |
28 |
| - # test downcasting |
| 27 | +class TestPossiblyDowncast(tm.TestCase): |
29 | 28 |
|
30 |
| - arr = np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995]) |
31 |
| - result = _possibly_downcast_to_dtype(arr, 'infer') |
32 |
| - assert (np.array_equal(result, arr)) |
| 29 | + def test_downcast_conv(self): |
| 30 | + # test downcasting |
33 | 31 |
|
34 |
| - arr = np.array([8., 8., 8., 8., 8.9999999999995]) |
35 |
| - result = _possibly_downcast_to_dtype(arr, 'infer') |
36 |
| - expected = np.array([8, 8, 8, 8, 9]) |
37 |
| - assert (np.array_equal(result, expected)) |
38 |
| - |
39 |
| - arr = np.array([8., 8., 8., 8., 9.0000000000005]) |
40 |
| - result = _possibly_downcast_to_dtype(arr, 'infer') |
41 |
| - expected = np.array([8, 8, 8, 8, 9]) |
42 |
| - assert (np.array_equal(result, expected)) |
43 |
| - |
44 |
| - # conversions |
| 32 | + arr = np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995]) |
| 33 | + result = _possibly_downcast_to_dtype(arr, 'infer') |
| 34 | + assert (np.array_equal(result, arr)) |
45 | 35 |
|
46 |
| - expected = np.array([1, 2]) |
47 |
| - for dtype in [np.float64, object, np.int64]: |
48 |
| - arr = np.array([1.0, 2.0], dtype=dtype) |
| 36 | + arr = np.array([8., 8., 8., 8., 8.9999999999995]) |
49 | 37 | result = _possibly_downcast_to_dtype(arr, 'infer')
|
50 |
| - tm.assert_almost_equal(result, expected, check_dtype=False) |
| 38 | + expected = np.array([8, 8, 8, 8, 9]) |
| 39 | + assert (np.array_equal(result, expected)) |
51 | 40 |
|
52 |
| - for dtype in [np.float64, object]: |
53 |
| - expected = np.array([1.0, 2.0, np.nan], dtype=dtype) |
54 |
| - arr = np.array([1.0, 2.0, np.nan], dtype=dtype) |
| 41 | + arr = np.array([8., 8., 8., 8., 9.0000000000005]) |
55 | 42 | result = _possibly_downcast_to_dtype(arr, 'infer')
|
56 |
| - tm.assert_almost_equal(result, expected) |
57 |
| - |
58 |
| - # empties |
59 |
| - for dtype in [np.int32, np.float64, np.float32, np.bool_, |
60 |
| - np.int64, object]: |
61 |
| - arr = np.array([], dtype=dtype) |
62 |
| - result = _possibly_downcast_to_dtype(arr, 'int64') |
63 |
| - tm.assert_almost_equal(result, np.array([], dtype=np.int64)) |
64 |
| - assert result.dtype == np.int64 |
| 43 | + expected = np.array([8, 8, 8, 8, 9]) |
| 44 | + assert (np.array_equal(result, expected)) |
| 45 | + |
| 46 | + # conversions |
| 47 | + |
| 48 | + expected = np.array([1, 2]) |
| 49 | + for dtype in [np.float64, object, np.int64]: |
| 50 | + arr = np.array([1.0, 2.0], dtype=dtype) |
| 51 | + result = _possibly_downcast_to_dtype(arr, 'infer') |
| 52 | + tm.assert_almost_equal(result, expected, check_dtype=False) |
| 53 | + |
| 54 | + for dtype in [np.float64, object]: |
| 55 | + expected = np.array([1.0, 2.0, np.nan], dtype=dtype) |
| 56 | + arr = np.array([1.0, 2.0, np.nan], dtype=dtype) |
| 57 | + result = _possibly_downcast_to_dtype(arr, 'infer') |
| 58 | + tm.assert_almost_equal(result, expected) |
| 59 | + |
| 60 | + # empties |
| 61 | + for dtype in [np.int32, np.float64, np.float32, np.bool_, |
| 62 | + np.int64, object]: |
| 63 | + arr = np.array([], dtype=dtype) |
| 64 | + result = _possibly_downcast_to_dtype(arr, 'int64') |
| 65 | + tm.assert_almost_equal(result, np.array([], dtype=np.int64)) |
| 66 | + assert result.dtype == np.int64 |
| 67 | + |
| 68 | + def test_datetimelikes_nan(self): |
| 69 | + arr = np.array([1, 2, np.nan]) |
| 70 | + exp = np.array([1, 2, np.datetime64('NaT')], dtype='datetime64[ns]') |
| 71 | + res = _possibly_downcast_to_dtype(arr, 'datetime64[ns]') |
| 72 | + tm.assert_numpy_array_equal(res, exp) |
| 73 | + |
| 74 | + exp = np.array([1, 2, np.timedelta64('NaT')], dtype='timedelta64[ns]') |
| 75 | + res = _possibly_downcast_to_dtype(arr, 'timedelta64[ns]') |
| 76 | + tm.assert_numpy_array_equal(res, exp) |
65 | 77 |
|
66 | 78 |
|
67 | 79 | class TestInferDtype(tm.TestCase):
|
|
0 commit comments