| 1176 | |
| 1177 | @pytest.mark.numpy |
| 1178 | def test_recordbatch_to_tensor_null(): |
| 1179 | arr1 = [1, 2, 3, 4, None, 6, 7, 8, 9] |
| 1180 | arr2 = [10, 20, 30, 40, 50, 60, 70, None, 90] |
| 1181 | batch = pa.RecordBatch.from_arrays( |
| 1182 | [ |
| 1183 | pa.array(arr1, type=pa.int32()), |
| 1184 | pa.array(arr2, type=pa.float32()), |
| 1185 | ], ["a", "b"] |
| 1186 | ) |
| 1187 | with pytest.raises( |
| 1188 | pa.ArrowTypeError, |
| 1189 | match="Can only convert a Table or RecordBatch with no nulls." |
| 1190 | ): |
| 1191 | batch.to_tensor() |
| 1192 | |
| 1193 | result = batch.to_tensor(null_to_nan=True, row_major=False) |
| 1194 | x = np.column_stack([arr1, arr2]).astype(np.float64, order="F") |
| 1195 | expected = pa.Tensor.from_numpy(x) |
| 1196 | |
| 1197 | np.testing.assert_equal(result.to_numpy(), x) |
| 1198 | assert result.size == 18 |
| 1199 | assert result.type == pa.float64() |
| 1200 | assert result.shape == expected.shape |
| 1201 | assert result.strides == expected.strides |
| 1202 | |
| 1203 | # int32 -> float64 |
| 1204 | batch = pa.RecordBatch.from_arrays( |
| 1205 | [ |
| 1206 | pa.array(arr1, type=pa.int32()), |
| 1207 | pa.array(arr2, type=pa.int32()), |
| 1208 | ], ["a", "b"] |
| 1209 | ) |
| 1210 | |
| 1211 | result = batch.to_tensor(null_to_nan=True, row_major=False) |
| 1212 | |
| 1213 | np.testing.assert_equal(result.to_numpy(), x) |
| 1214 | assert result.size == 18 |
| 1215 | assert result.type == pa.float64() |
| 1216 | assert result.shape == expected.shape |
| 1217 | assert result.strides == expected.strides |
| 1218 | |
| 1219 | # int8 -> float32 |
| 1220 | batch = pa.RecordBatch.from_arrays( |
| 1221 | [ |
| 1222 | pa.array(arr1, type=pa.int8()), |
| 1223 | pa.array(arr2, type=pa.int8()), |
| 1224 | ], ["a", "b"] |
| 1225 | ) |
| 1226 | |
| 1227 | result = batch.to_tensor(null_to_nan=True, row_major=False) |
| 1228 | x = np.column_stack([arr1, arr2]).astype(np.float32, order="F") |
| 1229 | expected = pa.Tensor.from_numpy(x) |
| 1230 | |
| 1231 | np.testing.assert_equal(result.to_numpy(), x) |
| 1232 | assert result.size == 18 |
| 1233 | assert result.type == pa.float32() |
| 1234 | assert result.shape == expected.shape |
| 1235 | assert result.strides == expected.strides |