@@ -307,10 +307,12 @@ def __init__(
307307 self ._representation = self ._infer_representation (
308308 representation_type , differential_type
309309 )
310- self . _data = self ._infer_data (args , copy , kwargs ) # possibly None.
310+ data = self ._infer_data (args , copy , kwargs ) # possibly None.
311311
312- # Set frame attributes, if any
312+ shapes = [] if data is None else [ data . shape ]
313313
314+ # Set frame attributes, if any.
315+ # Keep track of their shapes, but do not broadcast them yet.
314316 values = {}
315317 for fnm , fdefault in self .get_frame_attr_defaults ().items ():
316318 # Read-only frame attributes are defined as FrameAttribute
@@ -320,9 +322,9 @@ def __init__(
320322 if fnm in kwargs :
321323 value = kwargs .pop (fnm )
322324 setattr (self , "_" + fnm , value )
323- # Validate attribute by getting it. If the instance has data,
324- # this also checks its shape is OK. If not, we do it below.
325- values [ fnm ] = getattr (self , fnm )
325+ # Validate attribute by getting it.
326+ values [ fnm ] = value = getattr ( self , fnm )
327+ shapes . append ( getattr (value , "shape" , ()) )
326328 else :
327329 setattr (self , "_" + fnm , fdefault )
328330 self ._attr_names_with_defaults .append (fnm )
@@ -333,39 +335,25 @@ def __init__(
333335 f"keywords: { list (kwargs )} "
334336 )
335337
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 = ()
338+ # Determine the overall shape of the frame.
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
345+
346+ # Broadcast the data if necessary and set it
347+ if data is not None and data .shape != self ._shape :
348+ data = data ._apply (np .broadcast_to , shape = self ._shape , subok = True )
349+ self ._data = data
350+ # Broadcast the attributes if necessary by getting them again
351+ # (we now know the shapes will be OK).
352+ for key in values :
353+ getattr (self , key )
366354
367355 # The logic of this block is not related to the previous one
368- if self ._data is not None :
356+ if self .has_data :
369357 # This makes the cache keys backwards-compatible, but also adds
370358 # support for having differentials attached to the frame data
371359 # representation object.
@@ -721,7 +709,7 @@ def has_data(self):
721709
722710 @property
723711 def shape (self ):
724- return self .data . shape if self . has_data else self . _no_data_shape
712+ return self ._shape
725713
726714 # We have to override the ShapedLikeNDArray definitions, since our shape
727715 # does not have to be that of the data.
@@ -1609,6 +1597,7 @@ def apply_method(value):
16091597 new ._representation = self ._representation .copy ()
16101598 new ._attr_names_with_defaults = self ._attr_names_with_defaults .copy ()
16111599
1600+ new_shape = ()
16121601 for attr in self .frame_attributes :
16131602 _attr = "_" + attr
16141603 if attr in self ._attr_names_with_defaults :
@@ -1617,6 +1606,7 @@ def apply_method(value):
16171606 value = getattr (self , _attr )
16181607 if getattr (value , "shape" , ()):
16191608 value = apply_method (value )
1609+ new_shape = new_shape or value .shape
16201610 elif method == "copy" or method == "flatten" :
16211611 # flatten should copy also for a single element array, but
16221612 # we cannot use it directly for array scalars, since it
@@ -1627,22 +1617,11 @@ def apply_method(value):
16271617
16281618 if self .has_data :
16291619 new ._data = apply_method (self .data )
1620+ new_shape = new_shape or new ._data .shape
16301621 else :
16311622 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 = ()
1623+
1624+ new ._shape = new_shape
16461625
16471626 return new
16481627
0 commit comments