You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve error message when trying to assign data to a new column in empty table #4567
At the moment, in the case of an empty table, setting an existing column to a scalar produces no error:
In [17]: t=Table(names=['a'])
In [18]: t['a'] =1
but setting a non-existing column to a scalar produces an error:
In [19]: t['b'] =1---------------------------------------------------------------------------TypeErrorTraceback (mostrecentcalllast)
<ipython-input-19-e1b933d6b409>in<module>()
---->1t['b'] =1/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/astropy-1.1.dev14528-py3.5-macosx-10.5-x86_64.egg/astropy/table/table.pyin__setitem__(self, item, value)
11831184# Now add new column to the table->1185self.add_columns([new_column], copy=False)
11861187else:
/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/astropy-1.1.dev14528-py3.5-macosx-10.5-x86_64.egg/astropy/table/table.pyinadd_columns(self, cols, indexes, copy, rename_duplicate)
1524existing_names.add(new_name)
1525->1526self._init_from_cols(newcols)
15271528defreplace_column(self, name, col):
/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/astropy-1.1.dev14528-py3.5-macosx-10.5-x86_64.egg/astropy/table/table.pyin_init_from_cols(self, cols)
683"""Initialize table from a list of Column or mixin objects"""684-->685lengths=set(len(col) forcolincols)
686iflen(lengths) !=1:
687raiseValueError('Inconsistent data column lengths: {0}'/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/astropy-1.1.dev14528-py3.5-macosx-10.5-x86_64.egg/astropy/table/table.pyin<genexpr>(.0)
683"""Initialize table from a list of Column or mixin objects"""684-->685lengths=set(len(col) forcolincols)
686iflen(lengths) !=1:
687raiseValueError('Inconsistent data column lengths: {0}'TypeError: len() ofunsizedobject
The error message is not very clear. In the case of tables with rows, setting a new column to a scalar value does work. If one imagines a scenario where one is looping over tables, some empty, and some not, and doing t['b'] = 1 for all of them, should we be silently doing nothing except adding an empty column for the empty tables? Should we emit a better error message?
@astrofrog@mvnnn - I thought about this a bit and it seems that the most consistent behavior for your example is that t['b'] = 1 should create a zero-length column of the correct type. This happens to be the behavior of pandas DataFrame in this circumstance. So using the principle of least surprise, let's do that.
@taldcroft I think t['b'] = 1 will create a column having all row values as 1 rather than creating a column of zero-length. Isn't it? Correct me if I am wrong.
@sunilk747 - we are discussing the case where the table has no rows, i.e. all the columns have zero length. So "all row values as 1" is also correct, where "all" is a set of zero length.
Activity
taldcroft commentedon Feb 25, 2016
@astrofrog @mvnnn - I thought about this a bit and it seems that the most consistent behavior for your example is that
t['b'] = 1should create a zero-length column of the correct type. This happens to be the behavior of pandas DataFrame in this circumstance. So using the principle of least surprise, let's do that.mvnnn commentedon Mar 1, 2016
@taldcroft sorry, for ask again ..please describe more about what i need to do?.because i don't know
pandas DataFrame.taldcroft commentedon Mar 1, 2016
@mvnnn - you need to start by investigating pandas.
sunilk747 commentedon Mar 8, 2016
@taldcroft I think
t['b'] = 1will create a column having all row values as 1 rather than creating a column of zero-length. Isn't it? Correct me if I am wrong.taldcroft commentedon Mar 8, 2016
@sunilk747 - we are discussing the case where the table has no rows, i.e. all the columns have zero length. So "all row values as 1" is also correct, where "all" is a set of zero length.