Skip to content

Iterate over data_vars only #2506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ v0.11.0 (unreleased)
Breaking changes
~~~~~~~~~~~~~~~~

- Iterating over a ``Dataset`` now includes only data variables, not coordinates.
Similarily, calling ``len`` and ``bool`` on a ``Dataset`` now
includes only data variables
- Xarray's storage backends now automatically open and close files when
necessary, rather than requiring opening a file with ``autoclose=True``. A
global least-recently-used cache is used to store open files; the default
Expand Down
25 changes: 3 additions & 22 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,32 +930,13 @@ def __contains__(self, key):
return key in self._variables

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

def __bool__(self):
warnings.warn('casting an xarray.Dataset to a boolean will change in '
'xarray v0.11 to only include data variables, not '
'coordinates. Cast the Dataset.variables property '
'instead to preserve existing behavior in a forwards '
'compatible manner.',
FutureWarning, stacklevel=2)
return bool(self._variables)
return bool(self.data_vars)

def __iter__(self):
warnings.warn('iteration over an xarray.Dataset will change in xarray '
'v0.11 to only include data variables, not coordinates. '
'Iterate over the Dataset.variables property instead to '
'preserve existing behavior in a forwards compatible '
'manner.',
FutureWarning, stacklevel=2)
return iter(self._variables)
return iter(self.data_vars)

def __array__(self, dtype=None):
raise TypeError('cannot directly convert an xarray.Dataset into a '
Expand Down
12 changes: 4 additions & 8 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,12 @@ def test_properties(self):
assert isinstance(ds.dims.mapping, utils.SortedKeysDict)
assert type(ds.dims.mapping.mapping) is dict # noqa

with pytest.warns(FutureWarning):
assert list(ds) == list(ds.variables)
with pytest.warns(FutureWarning):
assert list(ds.keys()) == list(ds.variables)
assert list(ds) == list(ds.data_vars)
assert list(ds.keys()) == list(ds.data_vars)
assert 'aasldfjalskdfj' not in ds.variables
assert 'dim1' in repr(ds.variables)
with pytest.warns(FutureWarning):
assert len(ds) == 7
with pytest.warns(FutureWarning):
assert bool(ds)
assert len(ds) == 3
assert bool(ds)

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