`.sel` with a bare number silently selects by a different scale on a coordinate in degrees or percent
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 56/100
Hướng nghiên cứu
Start by reading PintIndex.sel and conversion.convert_indexer_units, then trace how array_convert_units handles bare numbers for index coordinates. Compare selection on scaled units such as degree and percent with radian and dimensionless, and review the existing indexer conversion tests. Done means the behavior is explicitly resolved and covered by tests for nearest selection and slices.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hello. Thanks for all your work on pint-xarray, it's awesome.
To be up front, this issue was identified using AI (Claude Opus 5.5) - I was only really looking at quantified data, not coords, but this came up during testing. This doesn't affect me directly but seems like a case for a warning & a docs update (which I'm happy to do if you agree).
Everything below was written & tested using AI. Cheers!
What happened
I understand that a bare number on a quantified index counts as dimensionless, and that a Quantity is the intended label.
The problem is that on a coordinate whose unit is compatible with dimensionless but scaled, such as degree, that rule gives no error or warning, and code that worked before .pint.quantify() silently selects different labels.
import pint_xarray # noqa: F401
import xarray as xr
da = xr.DataArray(
[1.0, 2.0, 3.0],
dims="lat",
coords={"lat": ("lat", [10.0, 20.0, 30.0], {"units": "degree"})},
)
quantified = da.pint.quantify()
print(da.sel(lat=20.0, method="nearest").lat.item()) # 20.0
print(quantified.sel(lat=20.0, method="nearest").lat.item()) # 30.0
print(da.sel(lat=slice(10.0, 20.0)).sizes["lat"]) # 2
print(quantified.sel(lat=slice(10.0, 20.0)).sizes["lat"]) # 0
What I expected
An error, as on a coordinate in metres, where the same mistake is caught:
xr.DataArray(
[1.0, 2.0],
dims="z",
coords={"z": ("z", [10.0, 20.0], {"units": "m"})},
).pint.quantify().sel(z=20.0)
# PintExceptionGroup: Cannot convert from 'dimensionless' (dimensionless) to 'meter' ([length])
Cause
PintIndex.sel passes its labels to conversion.convert_indexer_units, which ends in array_convert_units.
That treats a value that is not a quantity as dimensionless, as its docstring says and test_convert_indexer_units expects, and pint converts dimensionless to degrees by treating the number as radians.
So 20.0 becomes about 1146 degrees before the lookup:
method="nearest"then finds the largest label, 30.- The slice becomes roughly 573 to 1146 degrees, which is empty.
- An exact
.sel(lat=20.0)raisesKeyError, so that case is at least loud.
.sel with a real quantity is correct, as in quantified.sel(lat=20.0 * ureg.degree, method="nearest").
Which units are affected
Run on the same three labels, 10, 20 and 30:
| Coordinate unit | .sel(c=20.0, method="nearest") |
.sel(c=slice(10.0, 20.0)) size |
|---|---|---|
degree |
30 | 0 |
arcminute |
30 | 0 |
percent |
30 | 0 |
radian |
20 | 2 |
dimensionless |
20 | 2 |
| Unquantified, for comparison | 20 | 2 |
The accessor gives a different answer from the index: quantified.pint.sel(lat=20.0, method="nearest") returns the 20 degree row, since it has no indexer units to convert to and strips the coordinate's own.
Latitude and longitude are the common case, since degrees_north is compatible with dimensionless under cf-xarray's registry, and .sel(lat=..., method="nearest") is routine.
Question
Is this the behaviour you intend?
If it is, could the docs say so, since docs/creation.rst still says xarray does not support units in indexes, and nothing I could find says that labels need to be quantities?
If not, one option is to raise for a bare number on an index with a scaled unit, matching what already happens with metres.
Selection on dimensionless and radian could keep working, since the number means the same either way.
Versions
Reproduced in a clean environment with only pint-xarray and xarray installed:
| Package | Version |
|---|---|
| Python | 3.12 |
| pint-xarray | 0.6.1 |
| pint | 0.26.2 |
| xarray | 2026.9.0 |
| numpy | 2.5.4 |
convert_indexer_units and array_convert_units are unchanged on main.
The closest existing issues I found are #303, about method= being rejected, and #276, about indexing with quantities, which both concern a different failure.
- Ngôn ngữ chính
- Python
- Star
- 120
- Fork
- 17
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của xarray-contrib/pint-xarray
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
xarray-contrib/pint-xarray#362 · 1 bình luận ·
-
swap_dims drops coordinate unitsĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
xarray-contrib/pint-xarray#361 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
xarray-contrib/pint-xarray#303 · 3 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
xarray-contrib/pint-xarray#271 · 7 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
xarray-contrib/pint-xarray#269 · 3 bình luận ·
Tất cả issue của xarray-contrib/pint-xarray
Issue tương tự
-
documentation good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
MarketSquare/robotframework-browser#5479 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Visual COBOL: missing 11.0 and 12.0, and 9.0 date differs from Rocket Software's lifecycle pageĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
endoflife-date/endoflife.date#11561 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
OCA/l10n-brazil#5602 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation review-2.0.0
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dkuenzel/meshcore-home-gateway#106 ·
Maintainer thường phản hồi trong vòng 1 ngày