Integer dtypes and units can cause a footgun #3735
Replies: 5 comments 2 replies
-
|
Deviating from NumPy behavior is usually a bad idea, since it will be surprising for the majority of users and will require a lot of extra explanation and references in the docs. If you need the behavior you describe I suggest you create wrapper functions. |
Beta Was this translation helpful? Give feedback.
-
|
Then what is your opinion on the second option, making unit conversions automatically convert the dtype to float? |
Beta Was this translation helpful? Give feedback.
-
|
We could consider auto-converting in unit conversions when the factor is less than 1? And maybe add |
Beta Was this translation helpful? Give feedback.
-
The problem is still there if the conversion factor is larger than 1. Consider converting We could avoid converting the unit when the scaling factor is an integer. |
Beta Was this translation helpful? Give feedback.
-
|
A concrete example where this problem occurs, see here and here. If There are multiple other examples involving angels in the essreflectometry package, for example here. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When converting units the dtype is conserved, this can create some surprising behaviors:
To work around it we can pass
dtype='float64'as an extra argument in the unit conversion:We are doing lots of unit conversions like this in our workflows, to make them able to handle user inputs having different units, and it's likely that they fail silently as above if the user passes an integer argument instead of a float.
There are some cases when you might want to preserve dtype after a unit conversion, mainly when converting
counts, for example convertingcounts/stocounts/min(although in the opposite direction you do want a change of dtype!).The example above uses
degandradbecause that's a common case, but the same problem can happen with other units.If we don't want users having to be aware of this we could consider either:
float64by default unless the user explicitly specifies dtype.float64if the input is an integer type.What are your thoughts about it?
Beta Was this translation helpful? Give feedback.
All reactions