Polars Series.to_numpy() does not return ndarray

Question:

I was trying to convert a series to a numpy array via .to_numpy() but unlike what the documentation shows i am not getting a ndarray out but a seriesview

Running exactly the example in the documentation: https://pola-rs.github.io/polars/py-polars/html/reference/series/api/polars.Series.to_numpy.html

s = pl.Series("a", [1, 2, 3])
arr = s.to_numpy()
arr
type(arr)

I get

[1 2 3]
<class 'polars.series._numpy.SeriesView'>

Am i doing something wrong here and if not how should i work around this?

Asked By: J.N.

||

Answers:

It looks like the documentation is incorrect. Series.to_numpy() does return a series._numpy.SeriesView object, however by looking at the source code, series._numpy.SeriesView inherits the np.ndarray class and just adds another attribute to the class for internal purposses.

So for all purposes, there’s nothing you need to do. You can treat the return value of Series.to_numpy() as a np.ndarray because every polars.series._numpy.SeriesView is also a np.ndarray. Any method or attribute that an np.ndarray object has, it will also be present on a polars.series._numpy.SeriesView object.

Answered By: Michael M.