fvg
fvg

Reputation: 273

How to convert data for read_ipc / write_ipc in Polars?

I used .write_ipc from Polars to store as a feather file. It turns out that the numerical strings have been saved as integers.

So I need to convert the columns with integers to strings either before saving as feather or after reading from feather. How do I do that with Polars?

Upvotes: 3

Views: 17501

Answers (1)

ritchie46
ritchie46

Reputation: 14730

Are you sure? This snippets succeeds so that means data types are preserved.

import polars as pl
import io

df = pl.DataFrame({
    "numeric_strs": ["1", "2", "3"],
    "numeric_ints": [1, 2, 3]
})

f = io.BytesIO()
df.write_ipc(f)
f.seek(0)

assert pl.read_ipc(f).dtypes == [pl.String, pl.Int64]

To answer your question you can cast polars data types:

# cast to a string column
df.with_columns(pl.col("numeric_ints").cast(pl.String))

Upvotes: 7

Related Questions