Skip to content

Commit 180bbf2

Browse files
committed
Merge remote-tracking branch 'upstream/master' into map_blocks_2
* 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)
2 parents 765ca5d + df25933 commit 180bbf2

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

asv_bench/benchmarks/indexing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,16 @@ def setup(self, key):
125125
requires_dask()
126126
super().setup(key)
127127
self.ds = self.ds.chunk({"x": 100, "y": 50, "t": 50})
128+
129+
130+
class BooleanIndexing:
131+
# https://github.com/pydata/xarray/issues/2227
132+
def setup(self):
133+
self.ds = xr.Dataset(
134+
{"a": ("time", np.arange(10_000_000))},
135+
coords={"time": np.arange(10_000_000)},
136+
)
137+
self.time_filter = self.ds.time > 50_000
138+
139+
def time_indexing(self):
140+
self.ds.isel(time=self.time_filter)

doc/whats-new.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ New functions/methods
2424

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

2835
.. _whats-new.0.13.0:
2936

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

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

xarray/core/dataarray.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,15 @@ class DataArray(AbstractArray, DataWithCoords):
248248
Dictionary for holding arbitrary metadata.
249249
"""
250250

251-
__slots__ = ("_accessors", "_coords", "_file_obj", "_name", "_indexes", "_variable")
251+
__slots__ = (
252+
"_accessors",
253+
"_coords",
254+
"_file_obj",
255+
"_name",
256+
"_indexes",
257+
"_variable",
258+
"__weakref__",
259+
)
252260

253261
_groupby_cls = groupby.DataArrayGroupBy
254262
_rolling_cls = rolling.DataArrayRolling

xarray/core/dataset.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords):
420420
"_file_obj",
421421
"_indexes",
422422
"_variables",
423+
"__weakref__",
423424
)
424425

425426
_groupby_cls = groupby.DatasetGroupBy
@@ -1783,7 +1784,7 @@ def _validate_indexers(
17831784
elif isinstance(v, Dataset):
17841785
raise TypeError("cannot use a Dataset as an indexer")
17851786
elif isinstance(v, Sequence) and len(v) == 0:
1786-
v = IndexVariable((k,), np.zeros((0,), dtype="int64"))
1787+
v = Variable((k,), np.zeros((0,), dtype="int64"))
17871788
else:
17881789
v = np.asarray(v)
17891790

@@ -1797,16 +1798,13 @@ def _validate_indexers(
17971798
if v.ndim == 0:
17981799
v = Variable((), v)
17991800
elif v.ndim == 1:
1800-
v = IndexVariable((k,), v)
1801+
v = Variable((k,), v)
18011802
else:
18021803
raise IndexError(
18031804
"Unlabeled multi-dimensional array cannot be "
18041805
"used for indexing: {}".format(k)
18051806
)
18061807

1807-
if v.ndim == 1:
1808-
v = v.to_index_variable()
1809-
18101808
indexers_list.append((k, v))
18111809

18121810
return indexers_list
@@ -2369,7 +2367,10 @@ def interp(
23692367
if kwargs is None:
23702368
kwargs = {}
23712369
coords = either_dict_or_kwargs(coords, coords_kwargs, "interp")
2372-
indexers = OrderedDict(self._validate_indexers(coords))
2370+
indexers = OrderedDict(
2371+
(k, v.to_index_variable() if isinstance(v, Variable) and v.ndim == 1 else v)
2372+
for k, v in self._validate_indexers(coords)
2373+
)
23732374

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

xarray/tests/test_dataarray.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4672,3 +4672,14 @@ class MyArray(DataArray):
46724672
pass
46734673

46744674
assert str(e.value) == "MyArray must explicitly define __slots__"
4675+
4676+
4677+
def test_weakref():
4678+
"""Classes with __slots__ are incompatible with the weakref module unless they
4679+
explicitly state __weakref__ among their slots
4680+
"""
4681+
from weakref import ref
4682+
4683+
a = DataArray(1)
4684+
r = ref(a)
4685+
assert r() is a

xarray/tests/test_dataset.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5786,3 +5786,14 @@ class MyDS(Dataset):
57865786
pass
57875787

57885788
assert str(e.value) == "MyDS must explicitly define __slots__"
5789+
5790+
5791+
def test_weakref():
5792+
"""Classes with __slots__ are incompatible with the weakref module unless they
5793+
explicitly state __weakref__ among their slots
5794+
"""
5795+
from weakref import ref
5796+
5797+
ds = Dataset()
5798+
r = ref(ds)
5799+
assert r() is ds

0 commit comments

Comments
 (0)