Is it possible to access underlying data from Polars in cython?

Viewed 144

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?

3 Answers

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]
Related