This is an automated email from the ASF dual-hosted git repository.

kszucs pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 167bad1  ARROW-5790: [Python] Raise error when trying to convert 0-dim 
array in pa.array
167bad1 is described below

commit 167bad1a4db9f4aa597d90d6c4a5f8bcd41dccbf
Author: Joris Van den Bossche <jorisvandenboss...@gmail.com>
AuthorDate: Wed Jul 10 13:38:06 2019 +0200

    ARROW-5790: [Python] Raise error when trying to convert 0-dim array in 
pa.array
    
    https://issues.apache.org/jira/browse/ARROW-5790
    
    In the `NumPyConverter` constructor, we access the strides of the array 
with `PyArray_STRIDES(arr_)[0]`, so need to ensure that the array is 1D before 
passing it there.
    
    Author: Joris Van den Bossche <jorisvandenboss...@gmail.com>
    
    Closes #4837 from jorisvandenbossche/ARROW-5790 and squashes the following 
commits:
    
    cc66365af <Joris Van den Bossche> ARROW-5790:  raise error when trying to 
convert 0-dim array in pa.array
---
 cpp/src/arrow/python/numpy_to_arrow.cc |  3 +++
 python/pyarrow/tests/test_array.py     | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/cpp/src/arrow/python/numpy_to_arrow.cc 
b/cpp/src/arrow/python/numpy_to_arrow.cc
index b353a1e..811a31f 100644
--- a/cpp/src/arrow/python/numpy_to_arrow.cc
+++ b/cpp/src/arrow/python/numpy_to_arrow.cc
@@ -800,6 +800,9 @@ Status NdarrayToArrow(MemoryPool* pool, PyObject* ao, 
PyObject* mo, bool from_pa
   if (!PyArray_Check(ao)) {
     return Status::Invalid("Input object was not a NumPy array");
   }
+  if (PyArray_NDIM(reinterpret_cast<PyArrayObject*>(ao)) != 1) {
+    return Status::Invalid("only handle 1-dimensional arrays");
+  }
 
   NumPyConverter converter(pool, ao, mo, type, from_pandas, cast_options);
   RETURN_NOT_OK(converter.Convert());
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index f961c00..387b650 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -1147,6 +1147,17 @@ def test_array_from_masked():
         pa.array(ma, mask=np.array([True, False, False, False]))
 
 
+def test_array_from_invalid_dim_raises():
+    msg = "only handle 1-dimensional arrays"
+    arr2d = np.array([[1, 2, 3], [4, 5, 6]])
+    with pytest.raises(ValueError, match=msg):
+        pa.array(arr2d)
+
+    arr0d = np.array(0)
+    with pytest.raises(ValueError, match=msg):
+        pa.array(arr0d)
+
+
 def test_buffers_primitive():
     a = pa.array([1, 2, None, 4], type=pa.int16())
     buffers = a.buffers()

Reply via email to