@@ -4875,6 +4875,10 @@ def set_xindex(
4875
4875
"""Set a new, Xarray-compatible index from one or more existing
4876
4876
coordinate(s).
4877
4877
4878
+ All variables returned by the index's ``create_variables()`` method
4879
+ will be added to the dataset, including any additional variables
4880
+ beyond the original coordinate names.
4881
+
4878
4882
Parameters
4879
4883
----------
4880
4884
coord_names : str or list
@@ -4891,6 +4895,9 @@ def set_xindex(
4891
4895
-------
4892
4896
obj : Dataset
4893
4897
Another dataset, with this dataset's data and with a new index.
4898
+ The index will be associated with the coordinates specified in
4899
+ ``coord_names``. Any additional variables returned by the index's
4900
+ ``create_variables()`` method will also be added to the dataset.
4894
4901
4895
4902
"""
4896
4903
# the Sequence check is required for mypy
@@ -4947,10 +4954,13 @@ def set_xindex(
4947
4954
variables = self ._variables .copy ()
4948
4955
indexes = self ._indexes .copy ()
4949
4956
4950
- name = list (coord_names ).pop ()
4951
- if name in new_coord_vars :
4952
- variables [name ] = new_coord_vars [name ]
4953
- indexes [name ] = index
4957
+ # Add ALL variables returned by create_variables()
4958
+ for name , var in new_coord_vars .items ():
4959
+ variables [name ] = var
4960
+
4961
+ # Set index only for the original coordinate name
4962
+ coord_name = list (coord_names ).pop ()
4963
+ indexes [coord_name ] = index
4954
4964
else :
4955
4965
# reorder variables and indexes so that coordinates having the same
4956
4966
# index are next to each other
@@ -4964,11 +4974,12 @@ def set_xindex(
4964
4974
if name not in coord_names :
4965
4975
indexes [name ] = idx
4966
4976
4977
+ # Add ALL variables returned by create_variables()
4978
+ for name , var in new_coord_vars .items ():
4979
+ variables [name ] = var
4980
+
4981
+ # Set index for all original coordinate names
4967
4982
for name in coord_names :
4968
- try :
4969
- variables [name ] = new_coord_vars [name ]
4970
- except KeyError :
4971
- variables [name ] = self ._variables [name ]
4972
4983
indexes [name ] = index
4973
4984
4974
4985
return self ._replace (
0 commit comments