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-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new c677d4d3 feat(python): add back nanoarrow.array(..) constructor (#441)
c677d4d3 is described below
commit c677d4d396e75d362a626db6c56207ef4ee4befa
Author: Joris Van den Bossche <[email protected]>
AuthorDate: Tue Apr 23 21:15:49 2024 +0200
feat(python): add back nanoarrow.array(..) constructor (#441)
Closes https://github.com/apache/arrow-nanoarrow/issues/434
---------
Co-authored-by: Dewey Dunnington <[email protected]>
---
python/src/nanoarrow/__init__.py | 3 ++-
python/src/nanoarrow/array.py | 40 +++++++++++++++++++++++++++++++++++++++-
python/tests/test_array.py | 5 +++++
3 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/python/src/nanoarrow/__init__.py b/python/src/nanoarrow/__init__.py
index 5f99dc22..1e220932 100644
--- a/python/src/nanoarrow/__init__.py
+++ b/python/src/nanoarrow/__init__.py
@@ -73,7 +73,7 @@ from nanoarrow.schema import (
decimal256,
struct,
)
-from nanoarrow.array import Array
+from nanoarrow.array import array, Array
from nanoarrow._version import __version__ # noqa: F401
# Helps Sphinx automatically populate an API reference section
@@ -125,4 +125,5 @@ __all__ = [
"uint64",
"uint8",
"Array",
+ "array",
]
diff --git a/python/src/nanoarrow/array.py b/python/src/nanoarrow/array.py
index e38dc9c0..d3730e07 100644
--- a/python/src/nanoarrow/array.py
+++ b/python/src/nanoarrow/array.py
@@ -97,7 +97,7 @@ class Array:
The Array is nanoarrow's high-level in-memory array representation whose
scope maps to that of a fully-consumed ArrowArrayStream in the Arrow C Data
- interface. See :func:`array` for class details.
+ interface.
The :class:`Array` class is nanoarrow's high-level in-memory array
representation, encompasing the role of PyArrow's ``Array``,
@@ -498,3 +498,41 @@ class Array:
"""
self._assert_one_chunk("inspect")
print(_repr_utils.array_inspect(c_array(self)))
+
+
+def array(obj, schema=None, device=None) -> Array:
+ """
+ Create a nanoarrow.Array from array-like input.
+
+ The :class:`Array` class is nanoarrow's high-level in-memory array
+ representation whose scope maps to that of a fully-consumed
+ ArrowArrayStream in the Arrow C Data interface. Note that an
+ :class:`Array` is not necessarily contiguous in memory (i.e.,
+ it may consist of zero or more ``ArrowArray``s).
+ See :class:`Array` for class details.
+
+ Parameters
+ ----------
+ obj : array or array stream-like
+ An array-like or array stream-like object. This can be any object
+ supporting the Arrow PyCapsule interface, the Python buffer
+ protocol, or an iterable of Python objects.
+ schema : schema-like, optional
+ An optional schema. This can be a Schema object, or object
+ implementing the Arrow PyCapsule interface for schemas
+ (i.e. having the ``__arrow_c_schema__`` protocol method).
+ device : Device, optional
+ The device associated with the buffers held by this Array.
+ Defaults to the CPU device.
+
+ Examples
+ --------
+
+ >>> import nanoarrow as na
+ >>> na.array([1, 2, 3], na.int32())
+ nanoarrow.Array<int32>[3]
+ 1
+ 2
+ 3
+ """
+ return Array(obj, schema=schema, device=device)
diff --git a/python/tests/test_array.py b/python/tests/test_array.py
index 553a6350..f99b38f5 100644
--- a/python/tests/test_array.py
+++ b/python/tests/test_array.py
@@ -38,6 +38,11 @@ def test_array_construct():
iter(array)
+def test_array_constructor():
+ array = na.array([1, 2, 3], na.int32())
+ assert array.schema.type == na.Type.INT32
+
+
def test_array_empty():
array = na.Array([], na.int32())
assert array.schema.type == na.Type.INT32