Skip to content

Commit aa876cf

Browse files
authored
Leave empty slot when not using accessors
1 parent ee9da17 commit aa876cf

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

xarray/core/dataarray.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ class DataArray(AbstractArray, DataWithCoords):
249249
Dictionary for holding arbitrary metadata.
250250
"""
251251

252-
_accessors: Optional[Dict[str, Any]] # noqa
252+
_cache: Dict[str, Any]
253253
_coords: Dict[Any, Variable]
254254
_indexes: Optional[Dict[Hashable, pd.Index]]
255255
_name: Optional[Hashable]
256256
_variable: Variable
257257

258258
__slots__ = (
259-
"_accessors",
259+
"_cache",
260260
"_coords",
261261
"_file_obj",
262262
"_indexes",
@@ -373,7 +373,6 @@ def __init__(
373373
assert isinstance(coords, dict)
374374
self._coords = coords
375375
self._name = name
376-
self._accessors = None
377376

378377
# TODO(shoyer): document this argument, once it becomes part of the
379378
# public interface.

xarray/core/dataset.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,17 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords):
419419
coordinates used for label based indexing.
420420
"""
421421

422-
_accessors: Optional[Dict[str, Any]]
423422
_attrs: Optional[Dict[Hashable, Any]]
423+
_cache: Dict[str, Any]
424424
_coord_names: Set[Hashable]
425425
_dims: Dict[Hashable, int]
426426
_encoding: Optional[Dict[Hashable, Any]]
427427
_indexes: Optional[Dict[Hashable, pd.Index]]
428428
_variables: Dict[Hashable, Variable]
429429

430430
__slots__ = (
431-
"_accessors",
432431
"_attrs",
432+
"_cache",
433433
"_coord_names",
434434
"_dims",
435435
"_encoding",
@@ -535,7 +535,6 @@ def __init__(
535535
data_vars, coords, compat=compat
536536
)
537537

538-
self._accessors = None
539538
self._attrs = dict(attrs) if attrs is not None else None
540539
self._file_obj = None
541540
self._encoding = None
@@ -870,7 +869,6 @@ def _construct_direct(
870869
obj._attrs = attrs
871870
obj._file_obj = file_obj
872871
obj._encoding = encoding
873-
obj._accessors = None
874872
return obj
875873

876874
@classmethod

xarray/core/extensions.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ def __get__(self, obj, cls):
2020
# we're accessing the attribute of the class, i.e., Dataset.geo
2121
return self._accessor
2222

23+
# Use the same dict as @pandas.util.cache_readonly.
24+
# It must be explicitly declared in obj.__slots__.
2325
try:
24-
return obj._accessors[self._name]
25-
except TypeError:
26-
obj._accessors = {}
26+
cache = obj._cache
27+
except AttributeError:
28+
cache = obj._cache = {}
29+
30+
try:
31+
return cache[self._name]
2732
except KeyError:
2833
pass
2934

@@ -35,7 +40,7 @@ def __get__(self, obj, cls):
3540
# something else (GH933):
3641
raise RuntimeError("error initializing %r accessor." % self._name)
3742

38-
obj._accessors[self._name] = accessor_obj
43+
cache[self._name] = accessor_obj
3944
return accessor_obj
4045

4146

0 commit comments

Comments
 (0)