Skip to content

Commit 07b9856

Browse files
benbovyTomNicholas
authored andcommitted
as_variable: deprecate converting to IndexVariable
1 parent c9ba2be commit 07b9856

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

xarray/core/coordinates.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,9 @@ def __init__(
296296
variables = {k: v.copy() for k, v in coords.variables.items()}
297297
coords_obj_indexes = dict(coords.xindexes)
298298
else:
299-
variables = {}
300-
for name, data in coords.items():
301-
var = as_variable(data, name=name)
302-
if var.dims == (name,) and indexes is None:
303-
index, index_vars = create_default_index_implicit(var, list(coords))
304-
default_indexes.update({k: index for k in index_vars})
305-
variables.update(index_vars)
306-
else:
307-
variables[name] = var
299+
variables = {
300+
k: as_variable(v, name=k, auto_convert=False) for k, v in coords.items()
301+
}
308302

309303
if indexes is None:
310304
indexes = {}
@@ -998,7 +992,7 @@ def create_coords_with_default_indexes(
998992
if isinstance(obj, DataArray):
999993
dataarray_coords.append(obj.coords)
1000994

1001-
variable = as_variable(obj, name=name)
995+
variable = as_variable(obj, name=name, auto_convert=False)
1002996

1003997
if variable.dims == (name,):
1004998
idx, idx_vars = create_default_index_implicit(variable, all_variables)

xarray/core/dataarray.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ def _infer_coords_and_dims(
159159
dims = list(coords.keys())
160160
else:
161161
for n, (dim, coord) in enumerate(zip(dims, coords)):
162-
coord = as_variable(coord, name=dims[n]).to_index_variable()
162+
coord = as_variable(
163+
coord, name=dims[n], auto_convert=False
164+
).to_index_variable()
163165
dims[n] = coord.name
164166
dims_tuple = tuple(dims)
165167
if len(dims_tuple) != len(shape):
@@ -179,10 +181,12 @@ def _infer_coords_and_dims(
179181
new_coords = {}
180182
if utils.is_dict_like(coords):
181183
for k, v in coords.items():
182-
new_coords[k] = as_variable(v, name=k)
184+
new_coords[k] = as_variable(v, name=k, auto_convert=False)
185+
if new_coords[k].dims == (k,):
186+
new_coords[k] = new_coords[k].to_index_variable()
183187
elif coords is not None:
184188
for dim, coord in zip(dims_tuple, coords):
185-
var = as_variable(coord, name=dim)
189+
var = as_variable(coord, name=dim, auto_convert=False)
186190
var.dims = (dim,)
187191
new_coords[dim] = var.to_index_variable()
188192

xarray/core/merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def append_all(variables, indexes):
355355
indexes_.pop(name, None)
356356
append_all(coords_, indexes_)
357357

358-
variable = as_variable(variable, name=name)
358+
variable = as_variable(variable, name=name, auto_convert=False)
359359
if name in indexes:
360360
append(name, variable, indexes[name])
361361
elif variable.dims == (name,):

xarray/core/variable.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
decode_numpy_dict_values,
4242
drop_dims_from_indexers,
4343
either_dict_or_kwargs,
44+
emit_user_level_warning,
4445
ensure_us_time_resolution,
4546
is_duck_array,
4647
maybe_coerce_to_str,
@@ -84,7 +85,9 @@ class MissingDimensionsError(ValueError):
8485
# TODO: move this to an xarray.exceptions module?
8586

8687

87-
def as_variable(obj: T_DuckArray | Any, name=None) -> Variable | IndexVariable:
88+
def as_variable(
89+
obj: T_DuckArray | Any, name=None, auto_convert: bool = True
90+
) -> Variable | IndexVariable:
8891
"""Convert an object into a Variable.
8992
9093
Parameters
@@ -104,6 +107,9 @@ def as_variable(obj: T_DuckArray | Any, name=None) -> Variable | IndexVariable:
104107
along a dimension of this given name.
105108
- Variables with name matching one of their dimensions are converted
106109
into `IndexVariable` objects.
110+
auto_convert : bool, optional
111+
For internal use only! If True, convert a "dimension" variable into
112+
an IndexVariable object (deprecated).
107113
108114
Returns
109115
-------
@@ -154,9 +160,15 @@ def as_variable(obj: T_DuckArray | Any, name=None) -> Variable | IndexVariable:
154160
f"explicit list of dimensions: {obj!r}"
155161
)
156162

157-
if name is not None and name in obj.dims and obj.ndim == 1:
158-
# automatically convert the Variable into an Index
159-
obj = obj.to_index_variable()
163+
if auto_convert:
164+
if name is not None and name in obj.dims and obj.ndim == 1:
165+
# automatically convert the Variable into an Index
166+
emit_user_level_warning(
167+
f"variable {name!r} with name matching its dimension will not be "
168+
"automatically converted into an `IndexVariable` object in the future.",
169+
FutureWarning,
170+
)
171+
obj = obj.to_index_variable()
160172

161173
return obj
162174

@@ -749,8 +761,10 @@ def _broadcast_indexes_vectorized(self, key):
749761
variable = (
750762
value
751763
if isinstance(value, Variable)
752-
else as_variable(value, name=dim)
764+
else as_variable(value, name=dim, auto_convert=False)
753765
)
766+
if variable.dims == (dim,):
767+
variable = variable.to_index_variable()
754768
if variable.dtype.kind == "b": # boolean indexing case
755769
(variable,) = variable._nonzero()
756770

0 commit comments

Comments
 (0)