Skip to content

Deprecate checking coordinates in DataArray.__contains__ #1645

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, 2017
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
9 changes: 7 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ Breaking changes
disk when calling ``repr`` (:issue:`1522`).
By `Guido Imperiale <https://github.com/crusaderky>`_.

- ``Dataset.T`` has been deprecated an alias for ``Dataset.transpose()``
(:issue:`1232`).
- Deprecations:

- ``Dataset.T`` has been deprecated an alias for ``Dataset.transpose()``
(:issue:`1232`).
- ``key in data_array`` currently checks for membership in
``data_array.coords``. This is now deprecated: in the future, it will check
membership in ``data_array.values`` instead.


Backward Incompatible Changes
Expand Down
4 changes: 4 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def _attr_sources(self):
return [self.coords, LevelCoordinatesSource(self), self.attrs]

def __contains__(self, key):
warnings.warn(
'xarray.DataArray.__contains__ currently checks membership in '
'DataArray.coords, but in xarray v0.11 will change to check '
'membership in array values.', FutureWarning, stacklevel=2)
return key in self._coords

@property
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
raise TypeError('`group` must be an xarray.DataArray or the '
'name of an xarray variable or dimension')
group = obj[group]
if group.name not in obj and group.name in obj.dims:
if group.name not in obj.coords and group.name in obj.dims:
# DummyGroups should not appear on groupby results
group = _DummyGroup(obj, group.name, group.coords)

Expand Down
2 changes: 1 addition & 1 deletion xarray/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def apply(self, func, shortcut=False, **kwargs):
# If the aggregation function didn't drop the original resampling
# dimension, then we need to do so before we can rename the proxy
# dimension we used.
if self._dim in combined:
if self._dim in combined.coords:
combined = combined.drop(self._dim)

if self._resample_dim in combined.dims:
Expand Down
5 changes: 4 additions & 1 deletion xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ def reduce(self, func, **kwargs):
for _, window in self]

# Find valid windows based on count
concat_dim = self.window_labels if self.dim in self.obj else self.dim
if self.dim in self.obj.coords:
concat_dim = self.window_labels
else:
concat_dim = self.dim
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the only place we were looking for coords in this way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were a couple more. Verified by converting the warning into an error and running the test suite.

counts = concat([window.count(dim=self.dim) for _, window in self],
dim=concat_dim)
result = concat(windows, dim=concat_dim)
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ def test_setitem(self):
expected[t] = 1
self.assertArrayEqual(orig.values, expected)

def test_contains(self):
data_array = DataArray(1, coords={'x': 2})
with pytest.warns(FutureWarning):
assert 'x' in data_array

def test_attr_sources_multiindex(self):
# make sure attr-style access for multi-index levels
# returns DataArray objects
Expand Down