Skip to content

Commit c07bfbf

Browse files
committed
Better default behavior of the Coordinates constructor (pydata#8107)
* ``Coordinates.__init__`` create default indexes ... for any input dimension coordinate, if ``indexes=None``. Also, if another ``Coordinates`` object is passed, extract its indexes and raise if ``indexes`` is not None (no align/merge supported here). * add docstring examples * fix doctests * fix tests * update what's new
1 parent be99673 commit c07bfbf

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

xarray/core/coordinates.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,15 @@ 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-
k: as_variable(v, name=k, auto_convert=False) for k, v in coords.items()
301-
}
299+
variables = {}
300+
for name, data in coords.items():
301+
var = as_variable(data, name=name, auto_convert=False)
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
302308

303309
if indexes is None:
304310
indexes = {}

xarray/tests/test_coordinates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_from_pandas_multiindex(self) -> None:
8282
@pytest.mark.filterwarnings("ignore:return type")
8383
def test_dims(self) -> None:
8484
coords = Coordinates(coords={"x": [0, 1, 2]})
85-
assert set(coords.dims) == {"x"}
85+
assert coords.dims == {"x": 3}
8686

8787
def test_sizes(self) -> None:
8888
coords = Coordinates(coords={"x": [0, 1, 2]})

0 commit comments

Comments
 (0)