Skip to content

Commit 382d40d

Browse files
cactaurnamurphy
authored andcommitted
Raise TypeError when scalar value is passed to add_column.
Closes astropy#3811
1 parent fd4ffb5 commit 382d40d

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎astropy/table/table.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,9 +1942,13 @@ def add_column(self, col, index=None, name=None, rename_duplicate=False, copy=Tr
19421942
col = self._convert_data_to_col(col, name=name, copy=copy,
19431943
default_name=default_name)
19441944

1945+
# Assigning a scalar column to an empty table should result in an
1946+
# exception (see #3811).
1947+
if col.shape == () and len(self) == 0:
1948+
raise TypeError("Empty table cannot have column set to scalar value.")
19451949
# Make col data shape correct for scalars. The second test is to allow
19461950
# broadcasting an N-d element to a column, e.g. t['new'] = [[1, 2]].
1947-
if (col.shape == () or col.shape[0] == 1) and len(self) > 0:
1951+
elif (col.shape == () or col.shape[0] == 1) and len(self) > 0:
19481952
new_shape = (len(self),) + getattr(col, 'shape', ())[1:]
19491953
if isinstance(col, np.ndarray):
19501954
col = np.broadcast_to(col, shape=new_shape,
@@ -1953,6 +1957,7 @@ def add_column(self, col, index=None, name=None, rename_duplicate=False, copy=Tr
19531957
col = col._apply(np.broadcast_to, shape=new_shape,
19541958
subok=True)
19551959

1960+
19561961
# broadcast_to() results in a read-only array. Apparently it only changes
19571962
# the view to look like the broadcasted array. So copy.
19581963
col = col_copy(col)

0 commit comments

Comments
 (0)