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

how to avoid float/int casting error with to_reduced_units() on integer arrays #1184

Closed
CagtayFabry opened this issue Oct 8, 2020 · 0 comments · Fixed by #1417
Closed

how to avoid float/int casting error with to_reduced_units() on integer arrays #1184

CagtayFabry opened this issue Oct 8, 2020 · 0 comments · Fixed by #1417
Labels

Comments

@CagtayFabry
Copy link

What is the best approach to avoid casting errors when working with integer arrays? Is there any way to enforce casting to float for unit conversions?

The following example works with to_base_units() on all inputs but fails with to_reduced_units() once any kind of array gets introduced:

import pint
ureg = pint.UnitRegistry()
Q_ = ureg.Quantity

Q_(3, "s * ms").to_base_units() # works
Q_([3,4], "s * ms").to_base_units() # works, results are float

Q_(3, "s * ms").to_reduced_units() # works
Q_([3,4], "s * ms").to_reduced_units() # fails

UFuncTypeError: Cannot cast ufunc 'multiply' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'

to_reduced_units() seems call ito_reduced_units() almost immediately which produces the error.

With force_ndarray_like=True it obviously stops working for scalar inputs as well.

import pint
ureg = pint.UnitRegistry(force_ndarray_like=True)
Q_ = ureg.Quantity

Q_(3, "s * ms").to_reduced_units()

UFuncTypeError: Cannot cast ufunc 'multiply' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'

If possible I would like to catch this behavior as early as possible because it can be quite confusing for new users who might not consider explicitly casting their inputs as float and encounter this error at some later point during their analysis.

@jules-ch jules-ch added the bug label Nov 18, 2021
jules-ch added a commit to jules-ch/pint that referenced this issue Nov 18, 2021

Verified

This commit was signed with the committer’s verified signature.
jules-ch Jules Chéron
Do not use ito with to_reduced function
jules-ch added a commit to jules-ch/pint that referenced this issue Nov 18, 2021

Verified

This commit was signed with the committer’s verified signature.
jules-ch Jules Chéron
Do not use ito with to_reduced function
@jules-ch jules-ch mentioned this issue Nov 18, 2021
5 tasks
jules-ch added a commit to jules-ch/pint that referenced this issue Nov 18, 2021

Verified

This commit was signed with the committer’s verified signature.
jules-ch Jules Chéron
Do not use ito with to_reduced function
jules-ch added a commit to jules-ch/pint that referenced this issue Nov 23, 2021

Verified

This commit was signed with the committer’s verified signature.
jules-ch Jules Chéron
Do not use ito with to_reduced function
jules-ch added a commit to jules-ch/pint that referenced this issue Nov 23, 2021

Verified

This commit was signed with the committer’s verified signature.
jules-ch Jules Chéron
Do not use ito with to_reduced function
jules-ch added a commit to jules-ch/pint that referenced this issue Jan 15, 2022

Verified

This commit was signed with the committer’s verified signature.
jules-ch Jules Chéron
Do not use ito with to_reduced function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants