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
to_numpygives an array;to_pandasneeds pyarrow.from_pandasandfrom_numpycome back the other way.to_dictsis the plain-Python view, for small frames.
Keywords and builtins used here
aslistprinttype
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.
Step 2 of 2 in Reading & writing, step 30 of 32 in Polars.