使用astropy.table“OverflowError:Python int太大而无法转换为C long”

时间:2014-03-24 18:15:18

标签: python astropy

我想使用astropy.table读取一个简单的表。该行的第一个元素是一个大整数。它失败了,错误消息:“OverflowError:Python int太大而无法转换为C long”。我怎么能避免这个?

详细说明:

该表位于test.cat中。这很简单,一行: 81421100001 2 1 1 37.5991 1.0213 785.364 539.291

以下是我使用的代码:

import numpy as np
from astropy.table import Table

catalog_filename = 'test.cat'

t = Table.read(catalog_filename, format='ascii')

我收到以下错误:

Traceback (most recent call last):
  File "catread.py", line 15, in <module>
    t = Table.read(catalog_filename, format='ascii')
  File "/usr/local/lib/python2.7/dist-packages/astropy/table/table.py", line 2561, in read
    return io_registry.read(cls, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/registry.py", line 319, in read
    table = reader(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/connect.py", line 18, in read_asciitable
    return read(filename, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/ui.py", line 154, in read
    dat = _guess(table, new_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/ui.py", line 196, in _guess
    dat = reader.read(table)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 872, in read
    table = self.outputter(cols, self.meta)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 670, in __call__
    self._convert_vals(cols)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 652, in _convert_vals
    col.data = converter_func(col.str_vals)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 611, in converter
    return numpy.array(vals, numpy_type)
OverflowError: Python int too large to convert to C long

1 个答案:

答案 0 :(得分:1)

如上所述,这现在是一个天文问题(https://github.com/astropy/astropy/issues/2234),并且有一个建议的修复,如果发生溢出,它将自动回退到字符串。在此期间,您可以指示ascii.read函数使用特定的numpy dtype将列从文本字符串转换为final table列。使用下面的converters关键字arg。

>>> ascii.read(['8142110000100000000 1 2 3'], 
               converters={'col1': [ascii.convert_numpy(np.int64)]})
<Table rows=1 names=('col1','col2','col3','col4')>
array([(8142110000100000000, 1, 2, 3)], 
    dtype=[('col1', '<i8'), ('col2', '<i8'), ('col3', '<i8'), ('col4', '<i8')])

>>> ascii.read(['8142110000100000000 1 2 3'], 
                converters={'col1': [ascii.convert_numpy(np.float)]})
<Table rows=1 names=('col1','col2','col3','col4')>
array([(8.1421100001e+18, 1, 2, 3)], 
  dtype=[('col1', '<f8'), ('col2', '<i8'), ('col3', '<i8'), ('col4', '<i8')])