pythonformatpython-polars

Use format specifier to convert float/int column in polars dataframe to string


I have this code:

import polars as pl
df = pl.DataFrame({'size': [34.2399, 1232.22, -479.1]})
df.with_columns(pl.format('{:,.2f}', pl.col('size')))

But is fails:

ValueError - Traceback, line 3
      2 df = pl.DataFrame({'size': [34.2399, 1232.22, -479.1]})
----> 3 df.with_columns(pl.format('{:,.2f}', pl.col('size')))

File polars\functions\as_datatype.py:718, in format(f_string, *args)
    717     msg = "number of placeholders should equal the number of arguments"
--> 718     raise ValueError(msg)

ValueError: number of placeholders should equal the number of arguments

How can I format a float or int column using a format specifier like '{:,.2f}'?


Solution

  • As outlined by @mozway, general format strings are not yet supported as part of pl.format. The corresponding feature request already contains a nice polars implementation of (the most common) C-style sprint formatting.

    If efficiency is not too much of an issue (e.g. in exploratory data analysis), you can simply use pl.Expr.map_elements and fall back to the naive python solution.

    df.with_columns(
        pl.col("size").map_elements(lambda x: f"{x:,.2f}", return_dtype=pl.String)
    )
    
    shape: (3, 1)
    ┌──────────┐
    │ size     │
    │ ---      │
    │ str      │
    ╞══════════╡
    │ 34.24    │
    │ 1,232.22 │
    │ -479.10  │
    └──────────┘