Skip to content
forked from pydata/xarray

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into map_blocks_2
Browse files Browse the repository at this point in the history
* upstream/master:
  Fix isel performance regression (pydata#3319)
  Allow weakref (pydata#3318)
  Clarify that "scatter" is a plotting method in what's new. (pydata#3316)
  • Loading branch information
dcherian committed Sep 19, 2019
2 parents 765ca5d + df25933 commit 180bbf2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
13 changes: 13 additions & 0 deletions asv_bench/benchmarks/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,16 @@ def setup(self, key):
requires_dask()
super().setup(key)
self.ds = self.ds.chunk({"x": 100, "y": 50, "t": 50})


class BooleanIndexing:
# https://github.com/pydata/xarray/issues/2227
def setup(self):
self.ds = xr.Dataset(
{"a": ("time", np.arange(10_000_000))},
coords={"time": np.arange(10_000_000)},
)
self.time_filter = self.ds.time > 50_000

def time_indexing(self):
self.ds.isel(time=self.time_filter)
9 changes: 8 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ New functions/methods

- Added :py:func:`~xarray.map_blocks`, modeled after :py:func:`dask.array.map_blocks`
By `Deepak Cherian <https://github.com/dcherian>`_.
Bug fixes
~~~~~~~~~
- Reintroduce support for :mod:`weakref` (broken in v0.13.0). Support has been
reinstated for :class:`DataArray` and :class:`Dataset` objects only. Internal xarray
objects remain unaddressable by weakref in order to save memory.
(:issue:`3317`) by `Guido Imperiale <https://github.com/crusaderky>`_.


.. _whats-new.0.13.0:

Expand All @@ -32,7 +39,7 @@ v0.13.0 (17 Sep 2019)

This release includes many exciting changes: wrapping of
`NEP18 <https://www.numpy.org/neps/nep-0018-array-function-protocol.html>`_ compliant
numpy-like arrays; new :py:meth:`~Dataset.plot.scatter` method that can scatter
numpy-like arrays; new :py:meth:`~Dataset.plot.scatter` plotting method that can scatter
two ``DataArrays`` in a ``Dataset`` against each other; support for converting pandas
DataFrames to xarray objects that wrap ``pydata/sparse``; and more!

Expand Down
10 changes: 9 additions & 1 deletion xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ class DataArray(AbstractArray, DataWithCoords):
Dictionary for holding arbitrary metadata.
"""

__slots__ = ("_accessors", "_coords", "_file_obj", "_name", "_indexes", "_variable")
__slots__ = (
"_accessors",
"_coords",
"_file_obj",
"_name",
"_indexes",
"_variable",
"__weakref__",
)

_groupby_cls = groupby.DataArrayGroupBy
_rolling_cls = rolling.DataArrayRolling
Expand Down
13 changes: 7 additions & 6 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords):
"_file_obj",
"_indexes",
"_variables",
"__weakref__",
)

_groupby_cls = groupby.DatasetGroupBy
Expand Down Expand Up @@ -1783,7 +1784,7 @@ def _validate_indexers(
elif isinstance(v, Dataset):
raise TypeError("cannot use a Dataset as an indexer")
elif isinstance(v, Sequence) and len(v) == 0:
v = IndexVariable((k,), np.zeros((0,), dtype="int64"))
v = Variable((k,), np.zeros((0,), dtype="int64"))
else:
v = np.asarray(v)

Expand All @@ -1797,16 +1798,13 @@ def _validate_indexers(
if v.ndim == 0:
v = Variable((), v)
elif v.ndim == 1:
v = IndexVariable((k,), v)
v = Variable((k,), v)
else:
raise IndexError(
"Unlabeled multi-dimensional array cannot be "
"used for indexing: {}".format(k)
)

if v.ndim == 1:
v = v.to_index_variable()

indexers_list.append((k, v))

return indexers_list
Expand Down Expand Up @@ -2369,7 +2367,10 @@ def interp(
if kwargs is None:
kwargs = {}
coords = either_dict_or_kwargs(coords, coords_kwargs, "interp")
indexers = OrderedDict(self._validate_indexers(coords))
indexers = OrderedDict(
(k, v.to_index_variable() if isinstance(v, Variable) and v.ndim == 1 else v)
for k, v in self._validate_indexers(coords)
)

obj = self if assume_sorted else self.sortby([k for k in coords])

Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4672,3 +4672,14 @@ class MyArray(DataArray):
pass

assert str(e.value) == "MyArray must explicitly define __slots__"


def test_weakref():
"""Classes with __slots__ are incompatible with the weakref module unless they
explicitly state __weakref__ among their slots
"""
from weakref import ref

a = DataArray(1)
r = ref(a)
assert r() is a
11 changes: 11 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5786,3 +5786,14 @@ class MyDS(Dataset):
pass

assert str(e.value) == "MyDS must explicitly define __slots__"


def test_weakref():
"""Classes with __slots__ are incompatible with the weakref module unless they
explicitly state __weakref__ among their slots
"""
from weakref import ref

ds = Dataset()
r = ref(ds)
assert r() is ds

0 comments on commit 180bbf2

Please sign in to comment.