File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,12 @@ Deprecations
25
25
Bug fixes
26
26
~~~~~~~~~
27
27
28
+ - :py:meth: `Dataset.set_xindex ` now raises a helpful error when a custom index
29
+ creates extra variables that don't match the provided coordinate names, instead
30
+ of silently ignoring them. The error message suggests using the factory method
31
+ pattern with :py:meth: `xarray.Coordinates.from_xindex ` and
32
+ :py:meth: `Dataset.assign_coords ` for advanced use cases (:issue: `10499 `).
33
+ By `Dhruva Kumar Kaushal <https://github.com/dhruvak001 >`_.
28
34
29
35
Documentation
30
36
~~~~~~~~~~~~~
Original file line number Diff line number Diff line change @@ -4940,6 +4940,20 @@ def set_xindex(
4940
4940
if isinstance (index , PandasMultiIndex ):
4941
4941
coord_names = [index .dim ] + list (coord_names )
4942
4942
4943
+ # Check for extra variables that don't match the coordinate names
4944
+ extra_vars = set (new_coord_vars ) - set (coord_names )
4945
+ if extra_vars :
4946
+ extra_vars_str = ", " .join (f"'{ name } '" for name in extra_vars )
4947
+ coord_names_str = ", " .join (f"'{ name } '" for name in coord_names )
4948
+ raise ValueError (
4949
+ f"The index created extra variables { extra_vars_str } that are not "
4950
+ f"in the list of coordinates { coord_names_str } . "
4951
+ f"Use a factory method pattern instead:\n "
4952
+ f" index = { index_cls .__name__ } .from_variables(ds, { list (coord_names )!r} )\n "
4953
+ f" coords = xr.Coordinates.from_xindex(index)\n "
4954
+ f" ds = ds.assign_coords(coords)"
4955
+ )
4956
+
4943
4957
variables : dict [Hashable , Variable ]
4944
4958
indexes : dict [Hashable , Index ]
4945
4959
You can’t perform that action at this time.
0 commit comments