hgrecco / pint-pandas

Pandas support for pint
Other
170 stars 42 forks source link

Display of decimal places #74

Closed danolson1 closed 3 years ago

danolson1 commented 3 years ago

How do I control the number of decimal places that are displayed when Pint objects with float magnitudes are displayed in a Pandas DataFrame?

dcoukos commented 3 years ago

@danolson1 I think the function you are looking for is pandas.DataFrame.round.

In your case you might want to use your_df.round(decimals=your_decimals). If this doesn't work, then convert to float first with .astype(float)

danolson1 commented 3 years ago

I don't want it to actually round the values. df.style.format({'column_name': '{:.2f}'}) is a workaround, but it would be more convenient if there was a global option to set this for all pint data types.

andrewgsavage commented 3 years ago

You can use pint's string formatting options by setting the ureg's default format: pint_pandas.PintType.ureg.default_format = "~P" https://pint.readthedocs.io/en/stable/tutorial.html#string-formatting

danolson1 commented 3 years ago

Thanks. That's just what I was looking for.