Skip to content

Incorrect exception in Table value formatting if type and format mismatch #2868

@astrofrog

Description

@astrofrog

In the following example:

In [4]: from astropy.table import Table

In [5]: t = Table()

In [6]: t['a'] = ['a','b','c']

In [7]: t['a'].format = "%9.5f"

In [8]: print(t)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-d6c4f5caf31e> in <module>()
----> 1 print(t)

/Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9579-py3.4-macosx-10.8-x86_64.egg/astropy/table/table.py in __unicode__(self)
    664 
    665     def __unicode__(self):
--> 666         lines, n_header = self.formatter._pformat_table(self)
    667         return '\n'.join(lines)
    668     if six.PY3:

/Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9579-py3.4-macosx-10.8-x86_64.egg/astropy/table/pprint.py in _pformat_table(self, table, max_lines, max_width, show_name, show_unit, html, tableid)
    296         for col in six.itervalues(table.columns):
    297             lines, n_header = self._pformat_col(col, max_lines, show_name,
--> 298                                                 show_unit)
    299             cols.append(lines)
    300 

/Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9579-py3.4-macosx-10.8-x86_64.egg/astropy/table/pprint.py in _pformat_col(self, col, max_lines, show_name, show_unit)
    148         """
    149         outs = {}  # Some values from _pformat_col_iter iterator that are needed here
--> 150         col_strs = list(self._pformat_col_iter(col, max_lines, show_name, show_unit, outs))
    151         col_width = max(len(x) for x in col_strs)
    152 

/Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9579-py3.4-macosx-10.8-x86_64.egg/astropy/table/pprint.py in _pformat_col_iter(self, col, max_lines, show_name, show_unit, outs)
    239                                   format_func(col.format, col[(i,) + multidim1]))
    240                 else:
--> 241                     col_str = format_func(col.format, col[i])
    242                 yield col_str
    243             elif i == i0:

/Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9579-py3.4-macosx-10.8-x86_64.egg/astropy/table/pprint.py in _auto_format_func(format_, val)
     62             except:
     63                 raise ValueError('Unable to parse format string {0}'
---> 64                                  .format(format_))
     65     _format_funcs[format_] = format_func
     66     return out

ValueError: Unable to parse format string %9.5f

The exception is misleading. The issue is that the value needs to be a float. The 'proper' exception for this in Python is:

In [9]: "%9.5f" % 'a'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-55c5d51877aa> in <module>()
----> 1 "%9.5f" % 'a'

TypeError: a float is required

The current error makes it sound like the format is malformed.

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