Skip to content

Commit fc6f108

Browse files
committed
Allow broadcasting of frame attributes
Fixes astropy#8812.
1 parent ebb743e commit fc6f108

1 file changed

Lines changed: 2 additions & 27 deletions

File tree

‎astropy/coordinates/attributes.py‎

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,8 @@ def __get__(self, instance, frame_cls=None):
102102
out = getattr(instance, self.secondary_attribute, self.default)
103103

104104
out, converted = self.convert_input(out)
105-
if instance is not None:
106-
instance_shape = getattr(instance, 'shape', None) # None if instance (frame) has no data!
107-
if instance_shape is not None and (getattr(out, 'shape', ()) and
108-
out.shape != instance_shape):
109-
# If the shapes do not match, try broadcasting.
110-
try:
111-
if isinstance(out, ShapedLikeNDArray):
112-
out = out._apply(np.broadcast_to, shape=instance_shape,
113-
subok=True)
114-
else:
115-
out = np.broadcast_to(out, instance_shape, subok=True)
116-
except ValueError:
117-
# raise more informative exception.
118-
raise ValueError(
119-
"attribute {} should be scalar or have shape {}, "
120-
"but is has shape {} and could not be broadcast."
121-
.format(self.name, instance_shape, out.shape))
122-
123-
converted = True
105+
if instance is not None and converted:
106+
setattr(instance, '_' + self.name, out)
124107

125108
if converted:
126109
setattr(instance, '_' + self.name, out)
@@ -328,14 +311,6 @@ def convert_input(self, value):
328311

329312
oldvalue = value
330313
value = u.Quantity(oldvalue, self.unit, copy=False)
331-
if self.shape is not None and value.shape != self.shape:
332-
if value.shape == () and oldvalue == 0:
333-
# Allow a single 0 to fill whatever shape is needed.
334-
value = np.broadcast_to(value, self.shape, subok=True)
335-
else:
336-
raise ValueError(
337-
f'The provided value has shape "{value.shape}", but '
338-
f'should have shape "{self.shape}"')
339314

340315
converted = oldvalue is not value
341316
return value, converted

0 commit comments

Comments
 (0)