From 40b0c88838b692518fec80835c70233629010e31 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 15 Feb 2019 12:58:33 +0100 Subject: [PATCH] ARROW-3669: [Python] Raise error on Numpy byte-swapped array Previously we would return an incorrect result. Author: Antoine Pitrou Closes #3648 from pitrou/ARROW-3669-numpy-byteswapped-arrays and squashes the following commits: 1e0e10de ARROW-3669: Raise error on Numpy byte-swapped array --- cpp/src/arrow/python/numpy_to_arrow.cc | 5 +++++ python/pyarrow/tests/test_convert_pandas.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cpp/src/arrow/python/numpy_to_arrow.cc b/cpp/src/arrow/python/numpy_to_arrow.cc index ef63ccf83df11..8d15024805d7a 100644 --- a/cpp/src/arrow/python/numpy_to_arrow.cc +++ b/cpp/src/arrow/python/numpy_to_arrow.cc @@ -424,6 +424,11 @@ Status CopyStridedArray(PyArrayObject* arr, const int64_t length, MemoryPool* po template inline Status NumPyConverter::PrepareInputData(std::shared_ptr* data) { + if (PyArray_ISBYTESWAPPED(arr_)) { + // TODO + return Status::NotImplemented("Byte-swapped arrays not supported"); + } + if (is_strided()) { RETURN_NOT_OK(CopyStridedArray(arr_, length_, pool_, data)); } else if (dtype_->type_num == NPY_BOOL) { diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py index e9044866f8c27..fe5b3051a5bb4 100644 --- a/python/pyarrow/tests/test_convert_pandas.py +++ b/python/pyarrow/tests/test_convert_pandas.py @@ -607,6 +607,20 @@ def test_all_integer_types(self): arr = pa.array(np_arr) assert arr.to_pylist() == np_arr.tolist() + def test_integer_byteorder(self): + # Byteswapped arrays are not supported yet + int_dtypes = ['i1', 'i2', 'i4', 'i8', 'u1', 'u2', 'u4', 'u8'] + for dt in int_dtypes: + for order in '=<>': + data = np.array([1, 2, 42], dtype=order + dt) + for np_arr in (data, data[::2]): + if data.dtype.isnative: + arr = pa.array(data) + assert arr.to_pylist() == data.tolist() + else: + with pytest.raises(NotImplementedError): + arr = pa.array(data) + def test_integer_with_nulls(self): # pandas requires upcast to float dtype