Improve `Dataset.map` `keep_attrs`

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

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

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
8/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
活発
技術スタック
python
領域
data

調査の方向性

This is an open API design question, not a ready task: the issue asks maintainers to choose between three options for Dataset.map's keep_attrs. Start by reading the linked #11020, #11019 and #10726 threads to see what was already decided. Done means a maintainer-agreed design, so a newcomer should comment on the issue and wait for direction before writing code.

索引モデルが issue の本文から書いたものです。

説明

design question enhancement needs discussion
What is your issue?

Follow-up to #11020 / #11019.

Since #10726 (v2025.11.0) Dataset.map has no way to keep attributes set by the mapped function: keep_attrs=True overwrites them with the original attrs, keep_attrs=False drops them. #11020 fixes this regression by leaving the function's attrs alone for keep_attrs=False and merging with drop_conflicts for keep_attrs=True. The discussion there showed that the boolean keep_attrs is the wrong interface for map:

  • The function receives xarray objects, so it can handle attrs itself. keep_attrs mainly gets in the way of that (@dcherian, @max-sixty).
  • Coordinates can still be modified inside the function, so the coordinate attrs of the results need to be combined somehow. That is what the argument should control instead, e.g. as combine_attrs (@jsignell, @dcherian).
  • A boolean keep_attrs is ambiguous: does False mean "drop the input attrs" or "drop all attrs on the output"? (@keewis, @jsignell; see also #10997)
  • map is basically a loop over .data_vars, and users who need full control can do that loop themselves, e.g. ds.assign({name: func(var) for name, var in ds.data_vars.items()}) or xr.merge(..., combine_attrs=...) (@keewis, @jsignell).
  • Before deprecating keep_attrs, users need a way to opt into the new behavior (@keewis, @jsignell).

Options discussed for that:

  1. Add combine_attrs to map, accepting the usual strategy names / a callable, and deprecate keep_attrs. This is the option @keewis and @jsignell leaned towards.
  2. Let keep_attrs accept strategy names / a callable, like apply_ufunc, mapping True to "override" and False to "drop". This avoids a new argument, but a single value can't then control data variable and coordinate attrs separately.
  3. Remove keep_attrs from map without replacement and keep whatever attrs the function returns.
主要言語
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 を短くまとめたダイジェスト。