@@ -333,36 +333,15 @@ def __init__(
333333 f"keywords: { list (kwargs )} "
334334 )
335335
336- # We do ``is None`` because self._data might evaluate to false for
337- # empty arrays or data == 0
338- if self ._data is None :
339- # No data: we still need to check that any non-scalar attributes
340- # have consistent shapes. Collect them for all attributes with
341- # size > 1 (which should be array-like and thus have a shape).
342- shapes = {
343- fnm : value .shape
344- for fnm , value in values .items ()
345- if getattr (value , "shape" , ())
346- }
347- if shapes :
348- if len (shapes ) > 1 :
349- try :
350- self ._no_data_shape = check_broadcast (* shapes .values ())
351- except ValueError as err :
352- raise ValueError (
353- f"non-scalar attributes with inconsistent shapes: { shapes } "
354- ) from err
355-
356- # Above, we checked that it is possible to broadcast all
357- # shapes. By getting and thus validating the attributes,
358- # we verify that the attributes can in fact be broadcast.
359- for fnm in shapes :
360- getattr (self , fnm )
361- else :
362- self ._no_data_shape = shapes .popitem ()[1 ]
363-
364- else :
365- self ._no_data_shape = ()
336+ shapes = [getattr (value , "shape" , ()) for value in values .values ()]
337+ if self .has_data :
338+ shapes .append (getattr (self ._data , "shape" , ()))
339+ try :
340+ self ._shape = check_broadcast (* shapes )
341+ except ValueError as err :
342+ raise ValueError (
343+ f"non-scalar data and/or attributes with inconsistent shapes: { shapes } "
344+ ) from err
366345
367346 # The logic of this block is not related to the previous one
368347 if self ._data is not None :
@@ -721,7 +700,7 @@ def has_data(self):
721700
722701 @property
723702 def shape (self ):
724- return self .data . shape if self . has_data else self . _no_data_shape
703+ return self ._shape
725704
726705 # We have to override the ShapedLikeNDArray definitions, since our shape
727706 # does not have to be that of the data.
@@ -1597,8 +1576,10 @@ def _apply(self, method, *args, **kwargs):
15971576
15981577 def apply_method (value ):
15991578 if isinstance (value , ShapedLikeNDArray ):
1579+ value = value ._apply (np .broadcast_to , shape = self .shape , subok = True )
16001580 return value ._apply (method , * args , ** kwargs )
16011581 else :
1582+ value = np .broadcast_to (value , shape = self .shape , subok = True )
16021583 if callable (method ):
16031584 return method (value , * args , ** kwargs )
16041585 else :
@@ -1625,24 +1606,23 @@ def apply_method(value):
16251606
16261607 setattr (new , _attr , value )
16271608
1609+ shapes = [
1610+ getattr (new , "_" + attr ).shape
1611+ for attr in new .frame_attributes
1612+ if (
1613+ attr not in new ._attr_names_with_defaults
1614+ and getattr (getattr (new , "_" + attr ), "shape" , ())
1615+ )
1616+ ]
1617+
16281618 if self .has_data :
16291619 new ._data = apply_method (self .data )
1620+ if getattr (new ._data , "shape" , ()):
1621+ shapes .append (new ._data .shape )
16301622 else :
16311623 new ._data = None
1632- shapes = [
1633- getattr (new , "_" + attr ).shape
1634- for attr in new .frame_attributes
1635- if (
1636- attr not in new ._attr_names_with_defaults
1637- and getattr (getattr (new , "_" + attr ), "shape" , ())
1638- )
1639- ]
1640- if shapes :
1641- new ._no_data_shape = (
1642- check_broadcast (* shapes ) if len (shapes ) > 1 else shapes [0 ]
1643- )
1644- else :
1645- new ._no_data_shape = ()
1624+
1625+ new ._shape = check_broadcast (* shapes )
16461626
16471627 return new
16481628
0 commit comments