Skip to content

Commit

Permalink
ARROW-3669: [Python] Raise error on Numpy byte-swapped array
Browse files Browse the repository at this point in the history
Previously we would return an incorrect result.

Author: Antoine Pitrou <antoine@python.org>

Closes #3648 from pitrou/ARROW-3669-numpy-byteswapped-arrays and squashes the following commits:

1e0e10d <Antoine Pitrou> ARROW-3669:  Raise error on Numpy byte-swapped array
  • Loading branch information
pitrou authored and kszucs committed Feb 15, 2019
1 parent bf138a8 commit 40b0c88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cpp/src/arrow/python/numpy_to_arrow.cc
Expand Up @@ -424,6 +424,11 @@ Status CopyStridedArray(PyArrayObject* arr, const int64_t length, MemoryPool* po

template <typename ArrowType>
inline Status NumPyConverter::PrepareInputData(std::shared_ptr<Buffer>* data) {
if (PyArray_ISBYTESWAPPED(arr_)) {
// TODO
return Status::NotImplemented("Byte-swapped arrays not supported");
}

if (is_strided()) {
RETURN_NOT_OK(CopyStridedArray<ArrowType>(arr_, length_, pool_, data));
} else if (dtype_->type_num == NPY_BOOL) {
Expand Down
14 changes: 14 additions & 0 deletions python/pyarrow/tests/test_convert_pandas.py
Expand Up @@ -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

Expand Down

0 comments on commit 40b0c88

Please sign in to comment.