pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
30.53k stars 1.98k forks source link

Support decimal_separator in write_csv #19963

Open MarcSkovMadsen opened 10 hours ago

MarcSkovMadsen commented 10 hours ago

Description

Hi. I'm currently working on copy-paste functionality for HoloViz Panel in https://github.com/awesome-panel/panel-copy-paste.

I would like to be able to convert a Polars dataframe to tab separated \t csv strings and add them to the users clipboard for easy insertion into for example excel spreadsheets. I and some of my users are using European decimal_separator "," in Excel.

Please add option to use decimal_seperator "," in write_csv similarly to pandas decimal argument in to_csv.

Thanks.

Workaround

Convert to pandas

value.to_pandas().to_csv(sep="\t", decimal=decimal_separator, index=False)
ritchie46 commented 6 hours ago

An implementation for this would be rather expensive as the crate we use for formatting doesn't allow for this. And it is not something that is our priority of getting efficient.

Given that, I would recommend to use the engine to create the floats you want:

import polars.selectors as cs

pl.DataFrame({
    "a": [1.223, 12.443]
}).with_columns(cs.float().cast(pl.String).str.replace(".", ",", literal=True)).write_csv(..)