@@ -337,36 +337,15 @@ 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 = [getattr (value , "shape" , ()) for value in values .values ()]
341+ if self .has_data :
342+ shapes .append (getattr (self ._data , "shape" , ()))
343+ try :
344+ self ._shape = check_broadcast (* shapes )
345+ except ValueError as err :
346+ raise ValueError (
347+ f"non-scalar attributes with inconsistent shapes: { shapes } "
348+ ) from err
370349
371350 # The logic of this block is not related to the previous one
372351 if self ._data is not None :
@@ -725,7 +704,7 @@ def has_data(self):
725704
726705 @property
727706 def shape (self ):
728- return self .data . shape if self . has_data else self . _no_data_shape
707+ return self ._shape
729708
730709 # We have to override the ShapedLikeNDArray definitions, since our shape
731710 # does not have to be that of the data.
@@ -1601,8 +1580,10 @@ def _apply(self, method, *args, **kwargs):
16011580
16021581 def apply_method (value ):
16031582 if isinstance (value , ShapedLikeNDArray ):
1583+ value = value ._apply (np .broadcast_to , shape = self .shape , subok = True )
16041584 return value ._apply (method , * args , ** kwargs )
16051585 else :
1586+ value = np .broadcast_to (value , shape = self .shape , subok = True )
16061587 if callable (method ):
16071588 return method (value , * args , ** kwargs )
16081589 else :
@@ -1629,24 +1610,23 @@ def apply_method(value):
16291610
16301611 setattr (new , _attr , value )
16311612
1613+ shapes = [
1614+ getattr (new , "_" + attr ).shape
1615+ for attr in new .frame_attributes
1616+ if (
1617+ attr not in new ._attr_names_with_defaults
1618+ and getattr (getattr (new , "_" + attr ), "shape" , ())
1619+ )
1620+ ]
1621+
16321622 if self .has_data :
16331623 new ._data = apply_method (self .data )
1624+ if getattr (new ._data , "shape" , ()):
1625+ shapes .append (new ._data .shape )
16341626 else :
16351627 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 = ()
1628+
1629+ new ._shape = check_broadcast (* shapes )
16501630
16511631 return new
16521632
0 commit comments