xarray-contrib / xarray-contrib/pint-xarray
avoid setting `force_ndarray_like = True`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 119
- Forks
- 15
- PR merge metrics
- No merged PRs in 30d
Description
Moving to a separate issue to be able to properly track this.
Why does
pint-xarrayrequirepint.application_registry.force_ndarray_like = Truein the first place? I don't like when my dependencies force me to force something... This has become an issue because that very same setting is incompatible withpint-pandas: https://github.com/hgrecco/pint-pandas/issues/165. Given thatxarraybases onpandas, this seems to be somewhat serious.
Originally posted by @burnpanck in https://github.com/xarray-contrib/pint-xarray/issues/189#issuecomment-1532145519
The reason is that
xarraydetects a duck array using a certain interface, casting everything that does not satisfy that tonumpyusingnp.asarray. Since python scalars (float,int) don't have thendim,dtypeandshapeproperties (thoughpintdoes defaultndimto0), the result of somenumpyfunctions will be cast, dropping the units.Requiring
force_ndarray_likeavoids that, sincepintwill do the casting beforexarraycan do it. I agree that this feels like a hack, though.
Originally posted by @keewis in https://github.com/xarray-contrib/pint-xarray/issues/189#issuecomment-1542549750
Hm. I have a feeling that this is again a reason for implementing separate
pint.QuantityArrayandpint.QuantityScalartypes - because IMHOpintshould never respond tonp.asarrayby stripping units.
Either way,
pint-pandasdoes not require it, and managed to get most operations to work without stripping units. There, it is up to the user to make sure that their objects make it intopandasas aPintArray, e.g. by settingforce_ndarray_like = True, or simply by directly building aPintArray. But once the quantity arrays are in, pandas operations do not strip units (at least things improved significantly). Given that, in most cases, users of bothxarrayandpandaswork with arrays anyways, it feels like forcingforce_ndarray_like = Trueon every user is rather harsh. Even more so by doing it at import time - causing an import order dependency!
Originally posted by @burnpanck in https://github.com/xarray-contrib/pint-xarray/issues/189#issuecomment-1542765093
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the issue discussion, the xarray duck-array integration documentation, and the linked pint-pandas issue. Investigate where pint.application_registry.force_ndarray_like is required and how it is set at import time. Done should mean the dependency no longer forces this setting while preserving units and compatibility with both xarray and pint-pandas.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100