Is there anyway to access the underlying structures in cython/c++ for polars?
I have a number of scripts that grab np.ndarrays and iterates. Is there anything similar for polars?
Is there anyway to access the underlying structures in cython/c++ for polars?
I have a number of scripts that grab np.ndarrays and iterates. Is there anything similar for polars?
It should definitely be possible. Polars memory can be exported to pyarrow zero copy. And then you can use arrow's C data interface to get a hold of that memory.
Here is an example in the polars repo where they use the C data interface to get a hold of that memory again in Rust. https://github.com/pola-rs/polars/tree/master/examples/python_rust_compiled_function
polars.DataFrame([1,2,3]).to_arrow() will get a table that can be modified and edited.
cimport pyarrow
cimport pyarrow.lib
from libcpp.memory cimport shared_ptr
cdef iterate_through_table(polars_obj):
cdef:
shared_ptr[pyarrow.CTable] table = pyarrow.lib.unwrap_table(polars_obj.to_arrow())
The underlying data structures for polars are C++ arrays. However, you can access them using the Python API. For example, to get the underlying data for a polar, you can use the get_data()
method:
polar = some_polar.get_data()
You can then iterate over the data using the for
keyword:
for x, y in polar:
# do something with x and y
If you need to access the underlying C++ arrays directly, you can use the get_cpp_array()
method:
polar_array = some_polar.get_cpp_array()
You can then access the data using the []
operator:
x = polar_array[0]
y = polar_array[1]