@@ -337,36 +337,17 @@ def __init__(
337337 f"keywords: { list (kwargs )} "
338338 )
339339
340- # We do ``is None`` because self._data might evaluate to false for
341- # empty arrays or data == 0
342- if self ._data is None :
343- # No data: we still need to check that any non-scalar attributes
344- # have consistent shapes. Collect them for all attributes with
345- # size > 1 (which should be array-like and thus have a shape).
346- shapes = {
347- fnm : value .shape
348- for fnm , value in values .items ()
349- if getattr (value , "shape" , ())
350- }
351- if shapes :
352- if len (shapes ) > 1 :
353- try :
354- self ._no_data_shape = check_broadcast (* shapes .values ())
355- except ValueError as err :
356- raise ValueError (
357- f"non-scalar attributes with inconsistent shapes: { shapes } "
358- ) from err
359-
360- # Above, we checked that it is possible to broadcast all
361- # shapes. By getting and thus validating the attributes,
362- # we verify that the attributes can in fact be broadcast.
363- for fnm in shapes :
364- getattr (self , fnm )
365- else :
366- self ._no_data_shape = shapes .popitem ()[1 ]
367-
368- else :
369- self ._no_data_shape = ()
340+ shapes = [
341+ value .shape for value in values .values () if getattr (value , "shape" , ())
342+ ]
343+ if self ._data is not None and getattr (self ._data , "shape" , ()):
344+ shapes .append (self ._data .shape )
345+ try :
346+ self ._shape = check_broadcast (* shapes )
347+ except ValueError as err :
348+ raise ValueError (
349+ f"non-scalar attributes with inconsistent shapes: { shapes } "
350+ ) from err
370351
371352 # The logic of this block is not related to the previous one
372353 if self ._data is not None :
@@ -725,7 +706,7 @@ def has_data(self):
725706
726707 @property
727708 def shape (self ):
728- return self .data . shape if self . has_data else self . _no_data_shape
709+ return self ._shape
729710
730711 # We have to override the ShapedLikeNDArray definitions, since our shape
731712 # does not have to be that of the data.
@@ -1601,8 +1582,10 @@ def _apply(self, method, *args, **kwargs):
16011582
16021583 def apply_method (value ):
16031584 if isinstance (value , ShapedLikeNDArray ):
1585+ value = value ._apply (np .broadcast_to , shape = self .shape , subok = True )
16041586 return value ._apply (method , * args , ** kwargs )
16051587 else :
1588+ value = np .broadcast_to (value , shape = self .shape , subok = True )
16061589 if callable (method ):
16071590 return method (value , * args , ** kwargs )
16081591 else :
@@ -1629,24 +1612,23 @@ def apply_method(value):
16291612
16301613 setattr (new , _attr , value )
16311614
1615+ shapes = [
1616+ getattr (new , "_" + attr ).shape
1617+ for attr in new .frame_attributes
1618+ if (
1619+ attr not in new ._attr_names_with_defaults
1620+ and getattr (getattr (new , "_" + attr ), "shape" , ())
1621+ )
1622+ ]
1623+
16321624 if self .has_data :
16331625 new ._data = apply_method (self .data )
1626+ if getattr (new ._data , "shape" , ()):
1627+ shapes .append (new ._data .shape )
16341628 else :
16351629 new ._data = None
1636- shapes = [
1637- getattr (new , "_" + attr ).shape
1638- for attr in new .frame_attributes
1639- if (
1640- attr not in new ._attr_names_with_defaults
1641- and getattr (getattr (new , "_" + attr ), "shape" , ())
1642- )
1643- ]
1644- if shapes :
1645- new ._no_data_shape = (
1646- check_broadcast (* shapes ) if len (shapes ) > 1 else shapes [0 ]
1647- )
1648- else :
1649- new ._no_data_shape = ()
1630+
1631+ new ._shape = check_broadcast (* shapes )
16501632
16511633 return new
16521634
0 commit comments