【问题标题】:Index out of bound when removing columns from a table (astropy)从表中删除列时索引超出范围(astropy)
【发布时间】:2021-11-25 19:39:29
【问题描述】:

我终于想出了如何使用 astropy.io 中的“表格”模块来编辑适合表格,但我仍然遇到超出范围的错误。 给定一个 fit 文件中包含 75.000 行星系的表,我想将所有 ID = 13 的星系导出到一个新文件中。这是我的代码:

import numpy as np
from astropy.io import fits
from astropy.table import Table
import matplotlib.pyplot as plt

# Opening the file and printing it as a table

hdul = fits.open("data_SDSS_Info.fit")
t = Table.read(hdul)
print("Printing data_SDSS_Info in Table format...","\n")
print(t)

# Removing rows from the table and outputting the subsample into a new FITS file

for i in range(len(hdul[1].data)):
    if hdul[1].data[i][36] != 13:
        t.remove_rows(i)
        
t.write("subsample.fit",overwrite=True)
        
print("Printing subsample table...")
print(t)

我认为这可以正常工作并且我无法发现任何错误(尽管我确信有更有效的方法可以做到这一点) - 但是当我运行此代码时它仍然超级慢 ,最后返回:“索引 37781 超出轴 0 的范围,大小为 37780”

第 37781 行是否特别有问题,或者这是由什么奇怪的东西引起的?

如果我尝试通过将子样本放入普通数组中来提取子样本,则该行不会出现任何错误...

有什么想法吗?

【问题讨论】:

    标签: python astropy fits


    【解决方案1】:

    您正在通过删除行来修改循环内的表,但索引i 正在迭代到原始表的长度。而是尝试类似:

    remove_idxs = []
    for i in range(len(hdul[1].data)):
        if hdul[1].data[i][36] != 13:
            remove_idxs.append(i)
    
    t.remove_rows(remove_idxs)
    

    这也会快得多,因为每次调用 remove_rows 的费用都相对较高。

    【讨论】:

    • 为什么还要使用 remove_rows 而不是使用掩码进行索引?我想它可能会节省内存......
    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多