Subclasses cannot preserve their types during some binary ops and ufuncs combining DataArray and Dataset

オープン
#11,829 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@AtharvRG がすでに取り組んでいます。

2026年10月31日 から。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
38/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
numpy, python
領域
backend

調査の方向性

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 の本文から書いたものです。

説明

needs triage
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:

  1. xr.Dataset * CustomDataArray, where * can be any binary operation (e.g. multiplication, addition, subtraction)
  2. ufunc(xr.Dataset, CustomDataArray), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)
  3. 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

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pydata/xarray のほかの issue

pydata/xarray の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。