|
14 | 14 | (["1", "2", None], None, np.array(["1", "2", None])),
|
15 | 15 | (["1", "2", None], np.dtype("str"), np.array(["1", "2", None])),
|
16 | 16 | ([1, 2, None], np.dtype("str"), np.array(["1", "2", None])),
|
| 17 | + ], |
| 18 | +) |
| 19 | +def test_construct_1d_ndarray_preserving_na( |
| 20 | + values, dtype, expected, using_infer_string |
| 21 | +): |
| 22 | + result = sanitize_array(values, index=None, dtype=dtype) |
| 23 | + if using_infer_string and expected.dtype == object and dtype is None: |
| 24 | + tm.assert_extension_array_equal(result, pd.array(expected, dtype="str")) |
| 25 | + else: |
| 26 | + tm.assert_numpy_array_equal(result, expected) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize("dtype", ["m8[ns]", "M8[ns]"]) |
| 30 | +def test_construct_1d_ndarray_preserving_na_datetimelike(dtype): |
| 31 | + arr = np.arange(5, dtype=np.int64).view(dtype) |
| 32 | + expected = np.array(list(arr), dtype=object) |
| 33 | + assert all(isinstance(x, type(arr[0])) for x in expected) |
| 34 | + |
| 35 | + result = sanitize_array(arr, index=None, dtype=np.dtype(object)) |
| 36 | + tm.assert_numpy_array_equal(result, expected) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "values, dtype, expected", |
| 41 | + [ |
17 | 42 | (
|
18 | 43 | np.ma.masked_array([1, 2, 3], mask=[False, True, False]),
|
19 | 44 | "int64",
|
|
41 | 66 | ),
|
42 | 67 | ],
|
43 | 68 | )
|
44 |
| -def test_construct_1d_ndarray_preserving_na( |
45 |
| - values, dtype, expected, using_infer_string |
46 |
| -): |
47 |
| - result = sanitize_array(values, index=None, dtype=dtype) |
48 |
| - if using_infer_string and expected.dtype == object and dtype is None: |
49 |
| - tm.assert_extension_array_equal(result, pd.array(expected, dtype="str")) |
50 |
| - else: |
51 |
| - tm.assert_numpy_array_equal(result, expected) |
52 |
| - |
53 |
| - |
54 |
| -@pytest.mark.parametrize("dtype", ["m8[ns]", "M8[ns]"]) |
55 |
| -def test_construct_1d_ndarray_preserving_na_datetimelike(dtype): |
56 |
| - arr = np.arange(5, dtype=np.int64).view(dtype) |
57 |
| - expected = np.array(list(arr), dtype=object) |
58 |
| - assert all(isinstance(x, type(arr[0])) for x in expected) |
59 |
| - |
60 |
| - result = sanitize_array(arr, index=None, dtype=np.dtype(object)) |
61 |
| - tm.assert_numpy_array_equal(result, expected) |
62 |
| - |
63 |
| - |
64 | 69 | def test_sanitize_masked_array_with_masked_ea(values, dtype, expected):
|
65 | 70 | result = sanitize_array(values, dtype=dtype)
|
66 | 71 | tm.assert_numpy_array_equal(result, expected)
|
0 commit comments