typestar

Talking to numpy and pandas in Python

Arrow underneath means conversions are cheap, and sometimes zero-copy.

import numpy as np
import polars as pl

frame = pl.DataFrame({"tpm": [104, 98], "stars": [3, 3]})

array = frame.to_numpy()
print(array.shape, array.dtype)
print(frame["tpm"].to_numpy().mean())

as_pandas = frame.to_pandas()
print(type(as_pandas).__name__, list(as_pandas.columns))
print(pl.from_pandas(as_pandas).equals(frame))

print(pl.from_numpy(np.arange(4).reshape(2, 2), schema=["a", "b"]))
print(frame.to_dicts())

How it works

  1. to_numpy gives an array; to_pandas needs pyarrow.
  2. from_pandas and from_numpy come back the other way.
  3. to_dicts is the plain-Python view, for small frames.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
421
Tokens
148
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 81 seconds.

Type this snippet

Step 2 of 2 in Reading & writing, step 30 of 32 in Polars.

← Previous Next →