fix(python): Improve handling of decimal conversion with to_numpy
in the absence of pyarrow
#12888
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #12730.
Several things to note here:
numpy
does not actually support decimal! Consequently the caller should either cast to Float64 first (with the potential loss of exactness that implies) and then export to numpy, or accept that the resulting array will contain decimal values in an object-dtype array, which will be inefficient.The Series
to_numpy
method exposed a "use_pyarrow" parameter that the DataFrame method did not (but should have done). When converting from DataFrame theuse_pyarrow
parameter was implicitly True in the underlying Series passthrough; now it is exposed explicitly with the same default. Note that if the dtypes don't require it then we still do the conversion ourselves withoutpyarrow
, so there is no actual change from the previous default behaviour.Added a dedicated
is_decimal()
to the Python-side DataType methods (I already added the same for Rust a while back) as we frequently need to treat decimal a little differently to the other numeric types.Update
Can now do the decimal conversions with/without
pyarrow
installed; in both cases the result is anndarray
of "object" dtype.