Converting to and from NumPy arrays
NumPy can also work with Polars. You can convert back and forth between Polars and NumPy. Also, in cases where Polars lacks a particular function, we have the option to utilize NumPy instead while benefiting from efficient columnar operations via the NumPy API.
In this recipe, we’ll cover how to convert from and to NumPy arrays, as well as how to use NumPy functions directly in Polars.
Getting ready
You need NumPy installed for this recipe:
pip install numpy
How to do it...
Here’s how you can utilize NumPy in Polars. We’ll first create a NumPy array and then convert back and forth between Polars and NumPy.
- Create a NumPy array:
arr = np.array([[1,2,3], [4,5,6]])
- Create a Polars DataFrame from the NumPy array:
df = pl.from_numpy(arr, schema=['a', 'b'], orient='col') df
The preceding code will return the following output:
Figure 10.1 –...