Skip to content

Improve error message when trying to assign data to a new column in empty table #4567

Open

Description

@astrofrog

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
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-e1b933d6b409> in <module>()
----> 1 t['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.py in __setitem__(self, item, value)
   1183 
   1184             # Now add new column to the table
-> 1185             self.add_columns([new_column], copy=False)
   1186 
   1187         else:

/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.py in add_columns(self, cols, indexes, copy, rename_duplicate)
   1524                 existing_names.add(new_name)
   1525 
-> 1526         self._init_from_cols(newcols)
   1527 
   1528     def replace_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.py in _init_from_cols(self, cols)
    683         """Initialize table from a list of Column or mixin objects"""
    684 
--> 685         lengths = set(len(col) for col in cols)
    686         if len(lengths) != 1:
    687             raise ValueError('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.py in <genexpr>(.0)
    683         """Initialize table from a list of Column or mixin objects"""
    684 
--> 685         lengths = set(len(col) for col in cols)
    686         if len(lengths) != 1:
    687             raise ValueError('Inconsistent data column lengths: {0}'

TypeError: len() of unsized object

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?

cc @taldcroft

Activity

  1. taldcroft commented on Feb 25, 2016

    @taldcroft
    Member

    @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.

  2. mvnnn commented on Mar 1, 2016

    @mvnnn
    Contributor

    @taldcroft sorry, for ask again ..please describe more about what i need to do?.because i don't know pandas DataFrame.

  3. taldcroft commented on Mar 1, 2016

    @taldcroft
    Member

    @mvnnn - you need to start by investigating pandas.

  4. sunilk747 commented on Mar 8, 2016

    @sunilk747

    @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.

  5. taldcroft commented on Mar 8, 2016

    @taldcroft
    Member

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions