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

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


The following commit(s) were added to refs/heads/main by this push:
     new 2ebb3f70d8 GH-34411: [Python] Change array constructor to accept 
pyarrow array (#34275)
2ebb3f70d8 is described below

commit 2ebb3f70d8c8a14a8fea392f00cb2f6bb1490931
Author: Alenka Frim <[email protected]>
AuthorDate: Thu Mar 2 14:29:31 2023 +0100

    GH-34411: [Python] Change array constructor to accept pyarrow array (#34275)
    
    ### Rationale for this change
    
    Currently, `pyarrow.array` doesn't accept pyarrow Arrays and this PR adds a 
check to allow that.
    
    * Closes: #34411
    
    Authored-by: Alenka Frim <[email protected]>
    Signed-off-by: Joris Van den Bossche <[email protected]>
---
 python/pyarrow/array.pxi           | 12 ++++++++++--
 python/pyarrow/compute.py          |  6 ++++--
 python/pyarrow/tests/test_array.py | 26 ++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index b2fd413fc7..dca99af812 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -232,6 +232,11 @@ def array(object obj, type=None, mask=None, size=None, 
from_pandas=None,
     else:
         c_from_pandas = from_pandas
 
+    if isinstance(obj, Array):
+        if type is not None and not obj.type.equals(type):
+            obj = obj.cast(type, safe=safe, memory_pool=memory_pool)
+        return obj
+
     if hasattr(obj, '__arrow_array__'):
         return _handle_arrow_array_protocol(obj, type, mask, size)
     elif _is_array_like(obj):
@@ -904,7 +909,7 @@ cdef class Array(_PandasConvertible):
             result = self.ap.Diff(deref(other.ap))
         return frombytes(result, safe=True)
 
-    def cast(self, object target_type=None, safe=None, options=None):
+    def cast(self, object target_type=None, safe=None, options=None, 
memory_pool=None):
         """
         Cast array values to another data type
 
@@ -918,12 +923,15 @@ cdef class Array(_PandasConvertible):
             Whether to check for conversion errors such as overflow.
         options : CastOptions, default None
             Additional checks pass by CastOptions
+        memory_pool : MemoryPool, optional
+            memory pool to use for allocations during function execution.
 
         Returns
         -------
         cast : Array
         """
-        return _pc().cast(self, target_type, safe=safe, options=options)
+        return _pc().cast(self, target_type, safe=safe,
+                          options=options, memory_pool=memory_pool)
 
     def view(self, object target_type):
         """
diff --git a/python/pyarrow/compute.py b/python/pyarrow/compute.py
index 5eb681b5fb..e4daaf5e58 100644
--- a/python/pyarrow/compute.py
+++ b/python/pyarrow/compute.py
@@ -331,7 +331,7 @@ def _make_global_functions():
 _make_global_functions()
 
 
-def cast(arr, target_type=None, safe=None, options=None):
+def cast(arr, target_type=None, safe=None, options=None, memory_pool=None):
     """
     Cast array values to another data type. Can also be invoked as an array
     instance method.
@@ -345,6 +345,8 @@ def cast(arr, target_type=None, safe=None, options=None):
         Check for overflows or other unsafe conversions
     options : CastOptions, default None
         Additional checks pass by CastOptions
+    memory_pool : MemoryPool, optional
+        memory pool to use for allocations during function execution.
 
     Examples
     --------
@@ -395,7 +397,7 @@ def cast(arr, target_type=None, safe=None, options=None):
             options = CastOptions.unsafe(target_type)
         else:
             options = CastOptions.safe(target_type)
-    return call_function("cast", [arr], options)
+    return call_function("cast", [arr], options, memory_pool)
 
 
 def index(data, value, start=None, end=None, *, memory_pool=None):
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index fa3de35904..d974af99c6 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -3381,3 +3381,29 @@ def test_struct_array_sort():
         {"a": 5, "b": "foo"},
         None
     ]
+
+
+def test_array_accepts_pyarrow_array():
+    arr = pa.array([1, 2, 3])
+    result = pa.array(arr)
+    assert arr == result
+
+    # Test casting to a different type
+    result = pa.array(arr, type=pa.uint8())
+    expected = pa.array([1, 2, 3], type=pa.uint8())
+    assert expected == result
+    assert expected.type == pa.uint8()
+
+    # Test casting with safe keyword
+    arr = pa.array([2 ** 63 - 1], type=pa.int64())
+
+    with pytest.raises(pa.ArrowInvalid):
+        pa.array(arr, type=pa.int32())
+
+    expected = pa.array([-1], type=pa.int32())
+    result = pa.array(arr, type=pa.int32(), safe=False)
+    assert result == expected
+
+    # Test memory_pool keyword is accepted
+    result = pa.array(arr, memory_pool=pa.default_memory_pool())
+    assert arr == result

Reply via email to