Skip to content

Commit d77db21

Browse files
authored
Iterate over data_vars only (#2506)
* iterate over data_vars * whats new
1 parent b8c4b78 commit d77db21

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ v0.11.0 (unreleased)
3333
Breaking changes
3434
~~~~~~~~~~~~~~~~
3535

36+
- Iterating over a ``Dataset`` now includes only data variables, not coordinates.
37+
Similarily, calling ``len`` and ``bool`` on a ``Dataset`` now
38+
includes only data variables
3639
- Xarray's storage backends now automatically open and close files when
3740
necessary, rather than requiring opening a file with ``autoclose=True``. A
3841
global least-recently-used cache is used to store open files; the default

xarray/core/dataset.py

+3-22
Original file line numberDiff line numberDiff line change
@@ -930,32 +930,13 @@ def __contains__(self, key):
930930
return key in self._variables
931931

932932
def __len__(self):
933-
warnings.warn('calling len() on an xarray.Dataset will change in '
934-
'xarray v0.11 to only include data variables, not '
935-
'coordinates. Call len() on the Dataset.variables '
936-
'property instead, like ``len(ds.variables)``, to '
937-
'preserve existing behavior in a forwards compatible '
938-
'manner.',
939-
FutureWarning, stacklevel=2)
940-
return len(self._variables)
933+
return len(self.data_vars)
941934

942935
def __bool__(self):
943-
warnings.warn('casting an xarray.Dataset to a boolean will change in '
944-
'xarray v0.11 to only include data variables, not '
945-
'coordinates. Cast the Dataset.variables property '
946-
'instead to preserve existing behavior in a forwards '
947-
'compatible manner.',
948-
FutureWarning, stacklevel=2)
949-
return bool(self._variables)
936+
return bool(self.data_vars)
950937

951938
def __iter__(self):
952-
warnings.warn('iteration over an xarray.Dataset will change in xarray '
953-
'v0.11 to only include data variables, not coordinates. '
954-
'Iterate over the Dataset.variables property instead to '
955-
'preserve existing behavior in a forwards compatible '
956-
'manner.',
957-
FutureWarning, stacklevel=2)
958-
return iter(self._variables)
939+
return iter(self.data_vars)
959940

960941
def __array__(self, dtype=None):
961942
raise TypeError('cannot directly convert an xarray.Dataset into a '

xarray/tests/test_dataset.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,12 @@ def test_properties(self):
420420
assert isinstance(ds.dims.mapping, utils.SortedKeysDict)
421421
assert type(ds.dims.mapping.mapping) is dict # noqa
422422

423-
with pytest.warns(FutureWarning):
424-
assert list(ds) == list(ds.variables)
425-
with pytest.warns(FutureWarning):
426-
assert list(ds.keys()) == list(ds.variables)
423+
assert list(ds) == list(ds.data_vars)
424+
assert list(ds.keys()) == list(ds.data_vars)
427425
assert 'aasldfjalskdfj' not in ds.variables
428426
assert 'dim1' in repr(ds.variables)
429-
with pytest.warns(FutureWarning):
430-
assert len(ds) == 7
431-
with pytest.warns(FutureWarning):
432-
assert bool(ds)
427+
assert len(ds) == 3
428+
assert bool(ds)
433429

434430
assert list(ds.data_vars) == ['var1', 'var2', 'var3']
435431
assert list(ds.data_vars.keys()) == ['var1', 'var2', 'var3']

0 commit comments

Comments
 (0)