Subclasses cannot preserve their types during some binary ops and ufuncs combining DataArray and Dataset
メンテナーはふだん 1 日以内に返信
@AtharvRG がすでに取り組んでいます。
2026年10月31日 から。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 38/100
調査の方向性
Read xr.Dataset._binary_op and SupportsArithmetic.__array_ufunc__, then locate the existing tests for binary operations and ufuncs involving DataArray and Dataset. The proposed hooks and their exact API require maintainer agreement, so confirm the intended subclass protocol before editing. Done means the three listed combinations return the specialized subclass type while existing non-subclass behavior remains covered by tests.
索引モデルが issue の本文から書いたものです。
説明
What is your issue?
From uxarray downstream (UXARRAY/uxarray#1802). While xarray currently discourages subclassing in general, it does also point out uxarray in particular as an example where subclassing is used. In the context of uxarray, it is desirable and intuitive for binary operations and ufuncs involving a uxarray object and an xarray object to return a result which uses the more-specialized uxarray type.
The heart of the issue here is that xarray supports binary operations and binary ufuncs with one input being a Dataset and the other being a DataArray, but currently checks whether it should handle operations directly by checking isinstance(obj, xr.DataArray) or isinstance(obj, xr.Dataset) (which give True even for subclasses). When True, xarray fully handles the operations in a few cases, before subclasses ever get a chance to override anything. These cases are:
xr.Dataset * CustomDataArray, where * can be any binary operation (e.g. multiplication, addition, subtraction)ufunc(xr.Dataset, CustomDataArray), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)ufunc(xr.DataArray, CustomDataset), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)
All other combinations (such as xr.Dataset * CustomDataset, ufunc(xr.DataArray, CustomDataArray), and even xr.DataArray * CustomDataset) can be overridden by the subclass directly, returning the CustomDataset or CustomDataArray type one might hope for (UXARRAY/uxarray#1767 handled this for uxarray). However, these three remaining cases cannot fixed directly in the subclass, because:
- For (1), the operation goes directly to xr.Dataset's methods, and when xr.Dataset sees that the second term is an instance of xr.DataArray it decides to handle everything, without giving the second term a chance to alter the behavior in any way.
- For (2) and (3), when numpy applies the ufunc here it sees that the first term is not a subclass of the second term (or vice versa), so it starts checking for any
__array_ufunc__methods on the inputs, in order. This leads to applying the xarray object's__array_ufunc__, which similarly decides to handle everything after seeing that the second term is an instance of an xarray object (xr.DataArray in (2); xr.Dataset in (3)), also without giving the second term a chance to alter the behavior in any way.
One possible solution would be to introduce hooks or flags which do nothing in xarray, but which subclasses could override to regain control. For (1), I believe there would just need to be a hook inside xr.Dataset._binary_op, inside the if isinstance(other, DataArray | Dataset): block. It could probably be something as simple as if getattr(other, "_subclass_overrides_binary_op", False): return NotImplemented. For (2) and (3), I believe there would just need to be a hook inside SupportsArithmetic.__array_ufunc__, something like for x in inputs: if isinstance(x, SupportsArithmetic) and getattr(x, "_subclass_overrides_array_ufunc", False): return NotImplemented.
I would be very interested to open a PR to solve this issue, if xarray maintainers agree that something like this would sound reasonable. Also happy to take any suggestions on this topic, or clarify further if anything isn't clear!
Related: #3980
- 主要言語
- Python
- スター
- 4.2k
- フォーク
- 1.5k
- 平均マージ
- 2日 21時間
- マージ済み PR(30日)
- 74
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
pydata/xarray のほかの issue
-
xarray removes axis grid from pcolormesh対応中かも @Sealdot が今日担当しました。 オープンbug needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
`DataTree.map_over_datasets` behavior can be obscure sometimes対応中かも @mohansree14 が 24 日前に担当しました。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
pydata/xarray#11813 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identical対応中かも @SohamXYZDev が 29 日前に担当しました。 オープンbug needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
pydata/xarray#11777 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)対応中かも @d-chambers が 60 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
pydata/xarray#11598 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
DataTree constructor error message says dict even though dict is not accepted対応中かも @aryansk が 100 日前に担当しました。 オープンbug contrib-good-first-issue topic-DataTree topic-error reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
pydata/xarray#11514 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
abetlen/llama-cpp-python#2424 ·
メンテナーはふだん 1 日以内に返信
-
Bug IronPython Python 3
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
pyrevitlabs/pyRevit#4184 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信