You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2019/07/13 17:02:23 UTC

[arrow] 28/43: ARROW-5790: [Python] Raise error when trying to convert 0-dim array in pa.array

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

wesm pushed a commit to branch maint-0.14.x
in repository https://gitbox.apache.org/repos/asf/arrow.git

commit e4bdb6e968c51579d870174a4084885a5b0d2171
Author: Joris Van den Bossche <jo...@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 <jo...@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 9d66d96..34cfd29 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()