Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting output format temporarily #212

Open
kenahoo opened this issue Dec 4, 2023 · 1 comment
Open

Setting output format temporarily #212

kenahoo opened this issue Dec 4, 2023 · 1 comment

Comments

@kenahoo
Copy link

kenahoo commented Dec 4, 2023

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().

@andrewgsavage
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants