OverflowError:Python整数太大无法转换为C long" 与 astropy.table 相关

3 投票
1 回答
3719 浏览
提问于 2025-04-17 23:42

我想用 astropy.table 来读取一个简单的表格。这个表格的第一列是一个很大的整数。结果出现了错误,提示信息是:“OverflowError: Python int too large to convert to 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 个回答

1

如上所述,这个问题现在已经成为了一个astropy的议题(可以查看这个链接:https://github.com/astropy/astropy/issues/2234),并且已经有一个提议的解决办法,这个办法会在出现溢出时自动回退到字符串类型。与此同时,你可以告诉ascii.read这个函数,使用特定的numpy数据类型来将列从文本字符串转换为最终的表格列。可以像下面这样使用converters这个关键字参数。

>>> 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')])

撰写回答