| [ Web Proxy ] |
| Viewing: https://arrow.apache.org/docs/python/generated/../../cpp/../python/integration/extending.html | [Back] [Original] |
pyarrow provides both a Cython and C++ API, allowing your own native code to interact with pyarrow objects.
The Arrow C++ and PyArrow C++ header files are bundled with a pyarrow installation.
To get the absolute path to this directory (like numpy.get_include()), use:
import pyarrow as pa
pa.get_include()
Assuming the path above is on your compilers include path, the pyarrow API can be included using the following directive:
#include <arrow/python/pyarrow.h>
This will not include other parts of the Arrow API, which you will need
to include yourself (for example arrow/api.h).
When building C extensions that use the Arrow C++ libraries, you must add
appropriate linker flags. We have provided functions pa.get_libraries
and pa.get_library_dirs which return a list of library names and
likely library install locations (if you installed pyarrow with pip or
conda). These must be included when declaring your C extensions with
setuptools (see below).
Note
The PyArrow-specific C++ code is now a part of the PyArrow source tree
and not Arrow C++. That means the header files and arrow_python library
are not necessarily installed in the same location as that of Arrow C++ and
will no longer be automatically findable by CMake.
Initialize inner pointers of the pyarrow API. On success, 0 is returned. Otherwise, -1 is returned and a Python exception is set.
It is mandatory to call this function before calling any other function in the pyarrow C++ API. Failing to do so will likely lead to crashes.
pyarrow provides the following functions to go back and forth between Python wrappers (as exposed by the pyarrow Python API) and the underlying C++ objects.
Return whether obj wraps an Arrow C++ Array pointer;
in other words, whether obj is a pyarrow.Array instance.
Return whether obj wraps an Arrow C++ RecordBatch pointer;
in other words, whether obj is a pyarrow.RecordBatch instance.
Return whether obj wraps an Arrow C++ Buffer pointer;
in other words, whether obj is a pyarrow.Buffer instance.
Return whether obj wraps an Arrow C++ DataType pointer;
in other words, whether obj is a pyarrow.DataType instance.
Return whether obj wraps an Arrow C++ Field pointer;
in other words, whether obj is a pyarrow.Field instance.
Return whether obj wraps an Arrow C++ Scalar pointer;
in other words, whether obj is a pyarrow.Scalar instance.
Return whether obj wraps an Arrow C++ Schema pointer;
in other words, whether obj is a pyarrow.Schema instance.
Return whether obj wraps an Arrow C++ Table pointer;
in other words, whether obj is a pyarrow.Table instance.
Return whether obj wraps an Arrow C++ Tensor pointer;
in other words, whether obj is a pyarrow.Tensor instance.
Return whether obj wraps an Arrow C++ SparseCOOTensor pointer;
in other words, whether obj is a pyarrow.SparseCOOTensor instance.
Return whether obj wraps an Arrow C++ SparseCSCMatrix pointer;
in other words, whether obj is a pyarrow.SparseCSCMatrix instance.
Return whether obj wraps an Arrow C++ SparseCSFTensor pointer;
in other words, whether obj is a pyarrow.SparseCSFTensor instance.
Return whether obj wraps an Arrow C++ SparseCSRMatrix pointer;
in other words, whether obj is a pyarrow.SparseCSRMatrix instance.
The following functions expect a pyarrow object, unwrap the underlying
Arrow C++ API pointer, and return it as a Result object. An error
may be returned if the input object doesnt have the expected type.
Unwrap and return the Arrow C++ Array pointer from obj.
Unwrap and return the Arrow C++ RecordBatch pointer from obj.
Unwrap and return the Arrow C++ Buffer pointer from obj.
Unwrap and return the Arrow C++ DataType pointer from obj.
Unwrap and return the Arrow C++ Field pointer from obj.
Unwrap and return the Arrow C++ Scalar pointer from obj.
Unwrap and return the Arrow C++ Schema pointer from obj.
Unwrap and return the Arrow C++ Table pointer from obj.
Unwrap and return the Arrow C++ Tensor pointer from obj.
Unwrap and return the Arrow C++ SparseCOOTensor pointer from obj.
Unwrap and return the Arrow C++ SparseCSCMatrix pointer from obj.
Unwrap and return the Arrow C++ SparseCSFTensor pointer from obj.
Unwrap and return the Arrow C++ SparseCSRMatrix pointer from obj.
The following functions take an Arrow C++ API pointer and wrap it in a pyarray object of the corresponding type. A new reference is returned. On error, NULL is returned and a Python exception is set.
Wrap the Arrow C++ array in a pyarrow.Array instance.
Wrap the Arrow C++ record batch in a pyarrow.RecordBatch instance.
Wrap the Arrow C++ buffer in a pyarrow.Buffer instance.
Wrap the Arrow C++ data_type in a pyarrow.DataType instance.
Wrap the Arrow C++ field in a pyarrow.Field instance.
Wrap the Arrow C++ scalar in a pyarrow.Scalar instance.
Wrap the Arrow C++ schema in a pyarrow.Schema instance.
Wrap the Arrow C++ table in a pyarrow.Table instance.
Wrap the Arrow C++ tensor in a pyarrow.Tensor instance.
Wrap the Arrow C++ sparse_tensor in a pyarrow.SparseCOOTensor instance.
Wrap the Arrow C++ sparse_tensor in a pyarrow.SparseCSCMatrix instance.
Wrap the Arrow C++ sparse_tensor in a pyarrow.SparseCSFTensor instance.
Wrap the Arrow C++ sparse_tensor in a pyarrow.SparseCSRMatrix instance.
The Cython API more or less mirrors the C++ API, but the calling convention
can be different as required by Cython. In Cython, you dont need to
initialize the API as that will be handled automatically by the cimport
directive.
Note
Classes from the Arrow C++ API are renamed when exposed in Cython, to
avoid named clashes with the corresponding Python classes. For example,
C++ Arrow arrays have the CArray type and Array is the
corresponding Python wrapper class.
The following functions expect a pyarrow object, unwrap the underlying Arrow C++ API pointer, and return it. NULL is returned (without setting an exception) if the input is not of the right type.
Unwrap the Arrow C++ RecordBatch pointer from obj.
Unwrap the Arrow C++ Buffer pointer from obj.
Unwrap the Arrow C++ CDataType pointer from obj.
Unwrap the Arrow C++ Scalar pointer from obj.
Unwrap the Arrow C++ Schema pointer from obj.
Unwrap the Arrow C++ Tensor pointer from obj.
Unwrap the Arrow C++ SparseCOOTensor pointer from obj.
Unwrap the Arrow C++ SparseCSCMatrix pointer from obj.
Unwrap the Arrow C++ SparseCSFTensor pointer from obj.
Unwrap the Arrow C++ SparseCSRMatrix pointer from obj.
The following functions take a Arrow C++ API pointer and wrap it in a pyarray object of the corresponding type. An exception is raised on error.
Wrap the Arrow C++ array in a Python pyarrow.Array instance.
Wrap the Arrow C++ record batch in a Python pyarrow.RecordBatch instance.
Wrap the Arrow C++ buffer in a Python pyarrow.Buffer instance.
Wrap the Arrow C++ data_type in a Python pyarrow.DataType instance.
Wrap the Arrow C++ field in a Python pyarrow.Field instance.
Wrap the Arrow C++ resizable buffer in a Python pyarrow.ResizableBuffer instance.
Wrap the Arrow C++ scalar in a Python pyarrow.Scalar instance.
Wrap the Arrow C++ schema in a Python pyarrow.Schema instance.
Wrap the Arrow C++ table in a Python pyarrow.Table instance.
Wrap the Arrow C++ tensor in a Python pyarrow.Tensor instance.
Wrap the Arrow C++ COO sparse tensor in a Python pyarrow.SparseCOOTensor instance.
Wrap the Arrow C++ CSC sparse tensor in a Python pyarrow.SparseCSCMatrix instance.
Wrap the Arrow C++ COO sparse tensor in a Python pyarrow.SparseCSFTensor instance.
Wrap the Arrow C++ CSR sparse tensor in a Python pyarrow.SparseCSRMatrix instance.
The following Cython module shows how to unwrap a Python object and call the underlying C++ objects API.
# distutils: language=c++
from pyarrow.lib cimport *
def get_array_length(obj):
# Just an example function accessing both the pyarrow Cython API
# and the Arrow C++ API
cdef shared_ptr[CArray] arr = pyarrow_unwrap_array(obj)
if arr.get() == NULL:
raise TypeError("not an array")
return arr.get().length()
To build this module, you will need a slightly customized setup.py file
(this is assuming the file above is named example.pyx):
from setuptools import setup
from Cython.Build import cythonize
import os
import numpy as np
import pyarrow as pa
ext_modules = cythonize("example.pyx")
for ext in ext_modules:
# The Numpy C headers are currently required
ext.include_dirs.append(np.get_include())
ext.include_dirs.append(pa.get_include())
ext.libraries.extend(pa.get_libraries())
ext.library_dirs.extend(pa.get_library_dirs())
if os.name == 'posix':
ext.extra_compile_args.append('-std=c++20')
setup(ext_modules=ext_modules)
Compile the extension:
python setup.py build_ext --inplace
The Python wheels have the Arrow C++ libraries bundled in the top level
pyarrow/ install directory. On Linux and macOS, these libraries have an ABI
tag like libarrow.so.17 which means that linking with -larrow using the
linker path provided by pyarrow.get_library_dirs() will not work right out
of the box. To fix this, you must run pyarrow.create_library_symlinks()
once as a user with write access to the directory where pyarrow is
installed. This function will attempt to create symlinks like
pyarrow/libarrow.so. For example:
pip install pyarrow
python -c "import pyarrow; pyarrow.create_library_symlinks()"
The Python wheels for Linux are built using the
PyPA manylinux images which use
the AlmaLinux gcc-toolset-12. In addition to the other notes
above, if you are compiling C++ using these shared libraries, you will need
to make sure you use a compatible toolchain as well or you might see a
segfault during runtime.
| Web Proxy Viewer | New URL | Original Page |