@@ -1182,11 +1182,25 @@ def __dask_postpersist__(self):
11821182 def _rebuild (self , dsk , name = None ):
11831183 return Array (dsk , name or self .name , self .chunks , self .dtype , self ._meta )
11841184
1185- @property
1185+ def _reset_cache (self , key = None ):
1186+ """
1187+ Reset cached properties.
1188+
1189+ Parameters
1190+ ----------
1191+ key : str, optional
1192+ Remove specified key. The default removes all items.
1193+ """
1194+ if key is None :
1195+ self .__dict__ .clear ()
1196+ else :
1197+ self .__dict__ .pop (key , None )
1198+
1199+ @cached_property
11861200 def numblocks (self ):
11871201 return tuple (map (len , self .chunks ))
11881202
1189- @property
1203+ @cached_property
11901204 def npartitions (self ):
11911205 return reduce (mul , self .numblocks , 1 )
11921206
@@ -1260,16 +1274,15 @@ def _chunks(self):
12601274 def _chunks (self , chunks ):
12611275 self .__chunks = chunks
12621276
1263- # When the chunks changes the cached properties that was dependent
1264- # on it needs to be deleted:
1265- for v in ["shape" ]:
1266- if v in self .__dict__ :
1267- del self .__dict__ [v ]
1277+ # When the chunks changes the cached properties that was
1278+ # dependent on it needs to be deleted:
1279+ for key in ["numblocks" , "npartitions" , "shape" , "ndim" , "size" ]:
1280+ self ._reset_cache (key )
12681281
12691282 @property
12701283 def chunks (self ):
12711284 """Chunks property."""
1272- return self ._chunks
1285+ return self .__chunks
12731286
12741287 @chunks .setter
12751288 def chunks (self , chunks ):
@@ -1400,11 +1413,11 @@ def _repr_html_table(self):
14001413 ]
14011414 return "\n " .join (table )
14021415
1403- @property
1416+ @cached_property
14041417 def ndim (self ):
14051418 return len (self .shape )
14061419
1407- @property
1420+ @cached_property
14081421 def size (self ):
14091422 """ Number of elements in array """
14101423 return reduce (mul , self .shape , 1 )
0 commit comments