pint-pandas
pint-pandas copied to clipboard
Setting output format temporarily
If I want to set the units output format just for writing one output file, is there something less global than setting the pint.UnitRegistry.default_format
like below?
prev_format = pint.UnitRegistry.default_format
try:
pint.UnitRegistry.default_format = '~C'
df = df.pint.dequantify()
df.to_csv(file, index=False)
finally:
pint.UnitRegistry.default_format = prev_format
In this case, the code is handed a unit-aware DataFrame and needs to write it out, but it doesn't have direct access to the UnitRegistry
associated with it. It would be great if there's some property of df
that controlled the formatting, or perhaps an argument to to_csv()
.
not at present. I think
with pint.UnitRegistry.option_context('default_format', '~C'):
...
would be a nice way of going about this. Would need some changes in pint
another approach would be a format arguement in dequantify+quantify.