|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | from pandas._libs import iNaT |
| 7 | +from pandas.errors import Pandas4Warning |
7 | 8 |
|
8 | 9 | from pandas import array |
9 | 10 | import pandas._testing as tm |
|
15 | 16 | (np.int8, np.int16(127), np.int8), |
16 | 17 | (np.int8, np.int16(128), np.int16), |
17 | 18 | (np.int32, 1, np.int32), |
18 | | - (np.int32, 2.0, np.float64), |
| 19 | + (np.int32, 2.0, np.int32), |
19 | 20 | (np.int32, 3.0 + 4.0j, np.complex128), |
20 | 21 | (np.int32, True, np.object_), |
21 | 22 | (np.int32, "", np.object_), |
@@ -43,75 +44,104 @@ def dtype_fill_out_dtype(request): |
43 | 44 | class TestTake: |
44 | 45 | def test_1d_fill_nonna(self, dtype_fill_out_dtype): |
45 | 46 | dtype, fill_value, out_dtype = dtype_fill_out_dtype |
| 47 | + |
| 48 | + warn = None |
| 49 | + if out_dtype != dtype: |
| 50 | + warn = Pandas4Warning |
| 51 | + msg = "reindexing with a fill_value that cannot be held" |
| 52 | + |
46 | 53 | data = np.random.default_rng(2).integers(0, 2, 4).astype(dtype) |
47 | 54 | indexer = [2, 1, 0, -1] |
48 | 55 |
|
49 | | - result = algos.take_nd(data, indexer, fill_value=fill_value) |
| 56 | + with tm.assert_produces_warning(warn, match=msg): |
| 57 | + result = algos.take_nd(data, indexer, fill_value=fill_value) |
50 | 58 | assert (result[[0, 1, 2]] == data[[2, 1, 0]]).all() |
51 | 59 | assert result[3] == fill_value |
52 | 60 | assert result.dtype == out_dtype |
53 | 61 |
|
54 | 62 | indexer = [2, 1, 0, 1] |
55 | 63 |
|
56 | | - result = algos.take_nd(data, indexer, fill_value=fill_value) |
| 64 | + with tm.assert_produces_warning(warn, match=msg): |
| 65 | + result = algos.take_nd(data, indexer, fill_value=fill_value) |
57 | 66 | assert (result[[0, 1, 2, 3]] == data[indexer]).all() |
58 | 67 | assert result.dtype == dtype |
59 | 68 |
|
60 | 69 | def test_2d_fill_nonna(self, dtype_fill_out_dtype): |
61 | 70 | dtype, fill_value, out_dtype = dtype_fill_out_dtype |
| 71 | + |
| 72 | + warn = None |
| 73 | + if out_dtype != dtype: |
| 74 | + warn = Pandas4Warning |
| 75 | + msg = "reindexing with a fill_value that cannot be held" |
| 76 | + |
62 | 77 | data = np.random.default_rng(2).integers(0, 2, (5, 3)).astype(dtype) |
63 | 78 | indexer = [2, 1, 0, -1] |
64 | 79 |
|
65 | | - result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
| 80 | + with tm.assert_produces_warning(warn, match=msg): |
| 81 | + result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
66 | 82 | assert (result[[0, 1, 2], :] == data[[2, 1, 0], :]).all() |
67 | 83 | assert (result[3, :] == fill_value).all() |
68 | 84 | assert result.dtype == out_dtype |
69 | 85 |
|
70 | | - result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
| 86 | + with tm.assert_produces_warning(warn, match=msg): |
| 87 | + result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
71 | 88 | assert (result[:, [0, 1, 2]] == data[:, [2, 1, 0]]).all() |
72 | 89 | assert (result[:, 3] == fill_value).all() |
73 | 90 | assert result.dtype == out_dtype |
74 | 91 |
|
75 | 92 | indexer = [2, 1, 0, 1] |
76 | | - result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
| 93 | + with tm.assert_produces_warning(warn, match=msg): |
| 94 | + result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
77 | 95 | assert (result[[0, 1, 2, 3], :] == data[indexer, :]).all() |
78 | 96 | assert result.dtype == dtype |
79 | 97 |
|
80 | | - result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
| 98 | + with tm.assert_produces_warning(warn, match=msg): |
| 99 | + result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
81 | 100 | assert (result[:, [0, 1, 2, 3]] == data[:, indexer]).all() |
82 | 101 | assert result.dtype == dtype |
83 | 102 |
|
84 | 103 | def test_3d_fill_nonna(self, dtype_fill_out_dtype): |
85 | 104 | dtype, fill_value, out_dtype = dtype_fill_out_dtype |
86 | 105 |
|
| 106 | + warn = None |
| 107 | + if out_dtype != dtype: |
| 108 | + warn = Pandas4Warning |
| 109 | + msg = "reindexing with a fill_value that cannot be held" |
| 110 | + |
87 | 111 | data = np.random.default_rng(2).integers(0, 2, (5, 4, 3)).astype(dtype) |
88 | 112 | indexer = [2, 1, 0, -1] |
89 | 113 |
|
90 | | - result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
| 114 | + with tm.assert_produces_warning(warn, match=msg): |
| 115 | + result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
91 | 116 | assert (result[[0, 1, 2], :, :] == data[[2, 1, 0], :, :]).all() |
92 | 117 | assert (result[3, :, :] == fill_value).all() |
93 | 118 | assert result.dtype == out_dtype |
94 | 119 |
|
95 | | - result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
| 120 | + with tm.assert_produces_warning(warn, match=msg): |
| 121 | + result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
96 | 122 | assert (result[:, [0, 1, 2], :] == data[:, [2, 1, 0], :]).all() |
97 | 123 | assert (result[:, 3, :] == fill_value).all() |
98 | 124 | assert result.dtype == out_dtype |
99 | 125 |
|
100 | | - result = algos.take_nd(data, indexer, axis=2, fill_value=fill_value) |
| 126 | + with tm.assert_produces_warning(warn, match=msg): |
| 127 | + result = algos.take_nd(data, indexer, axis=2, fill_value=fill_value) |
101 | 128 | assert (result[:, :, [0, 1, 2]] == data[:, :, [2, 1, 0]]).all() |
102 | 129 | assert (result[:, :, 3] == fill_value).all() |
103 | 130 | assert result.dtype == out_dtype |
104 | 131 |
|
105 | 132 | indexer = [2, 1, 0, 1] |
106 | | - result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
| 133 | + with tm.assert_produces_warning(warn, match=msg): |
| 134 | + result = algos.take_nd(data, indexer, axis=0, fill_value=fill_value) |
107 | 135 | assert (result[[0, 1, 2, 3], :, :] == data[indexer, :, :]).all() |
108 | 136 | assert result.dtype == dtype |
109 | 137 |
|
110 | | - result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
| 138 | + with tm.assert_produces_warning(warn, match=msg): |
| 139 | + result = algos.take_nd(data, indexer, axis=1, fill_value=fill_value) |
111 | 140 | assert (result[:, [0, 1, 2, 3], :] == data[:, indexer, :]).all() |
112 | 141 | assert result.dtype == dtype |
113 | 142 |
|
114 | | - result = algos.take_nd(data, indexer, axis=2, fill_value=fill_value) |
| 143 | + with tm.assert_produces_warning(warn, match=msg): |
| 144 | + result = algos.take_nd(data, indexer, axis=2, fill_value=fill_value) |
115 | 145 | assert (result[:, :, [0, 1, 2, 3]] == data[:, :, indexer]).all() |
116 | 146 | assert result.dtype == dtype |
117 | 147 |
|
|
0 commit comments