[英]How can I turn an H5 table query result into a Astropy Table?
通常我用SQL查詢一個在線數據庫,但是數據庫宕機了。 我有一個 H5 文件,其中包含我需要查詢的表。 我使用Table.read_where('condition')
查詢了該表,並且對於符合我的標准的每一行都有一個numpy.void
元素列表。 有什么方法可以將該行列表放入 Astropy 表中? 這就是我以前使用的所有代碼,我寧願不必更改它。 這是我一直用來嘗試將其轉換為 Astropy 表的代碼:
import tables
from astropy.table import Table
import numpy as np
Data = tables.open_file('file_path','r') #opens our .h5 file
DataTable = Data.root.TableName #Points to the table
#Queries the table for rows that meet my 'Condition', and outputs a list of numpy.void's
#containing integers and floats. Each numpy.void represents a row in my table.
result = [row for row in DataTable.read_where('Condition')]
#I try to turn the list of rows into a Astropy table to use in the rest of my code.
resultTable = Table(rows=result,names=('Column1','Column2','Column3'))
我得到的錯誤是:
Traceback (most recent call last):
File "<ipython-input-2-aa9501cdbf2a>", line 1, in <module>
runfile('FilePath', wdir='FilePath')
File "Spyder File", line 827, in runfile
execfile(filename, namespace)
File "Spyder File", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "File Path", line 27, in <module>
resultTable = Table(rows=result,names=('Column1','Column2','Column3'))
File "FilePath/python3.7/site-packages/astropy/table/table.py", line 420, in __init__
rec_data = recarray_fromrecords(rows)
File "FilePath/lib/python3.7/site-packages/astropy/table/np_utils.py", line 196, in recarray_fromrecords
return np.rec.fromarrays(array_list, formats=formats)
File "FilePath/lib/python3.7/site-packages/numpy/core/records.py", line 645, in fromarrays
_array[_names[i]] = arrayList[i]
ValueError: Can't cast from structure to non-structure, except if the structure only has a single field.
我嘗試通過np.rec.fromrecords
傳遞result
以檢查這是否有效,因為 [Astropy 文檔] ( https://docs.astropy.org/en/stable/table/construct_table.html#construct-table ) 說必須能夠通過那個 function。 它可以正常工作,沒有任何錯誤。 我不確定 go 從這里到哪里。
我的替代計划是創建一個由result
中的行組成的 PyTables 表,並從中提取列為 numpy arrays 。 我寧願堅持只使用 Astropy,因為我使用的代碼是圍繞 Astropy 構建的,堅持使用它而不是通過並將其更改為 PyTables 會更容易。
根據AstroPy Table 的文檔,第一個參數可以是 NumPy 結構化數組或一維同構數組(相同類型)。
PyTables(表)function DataTable.read_where('Condition')
返回一個 NumPy 記錄數組,該數組匹配該表的描述(在 Z3B7F949B2343F9E5390E29F6 術語中稱為 dtype)。 因此,您想使用返回的數組來創建您的 AstroPy 表。 在 Pytables 調用中,您不需要row for row in
; 只需使用result = DataTable.read_where('Condition')
。
注意:默認情況下, astropy.table
將使用 NumPy 數組中的字段名稱。 您可以使用 ( print (result.dtype)
來查看它們。添加names=
參數會使用您提供的名稱覆蓋默認名稱。
更新的代碼顯示了以下更改:
Data = tables.open_file('file_path','r') #opens our .h5 file
DataTable = Data.root.TableName #Points to the PyTable table
#Queries the table for rows that meet my 'Condition', and
# returns a NumPy record array with dtype matching the table description
result = DataTable.read_where('Condition')
#reference the Numpy Array to create a AstroPy table for use in the rest of my code.
resultTable = Table(rows=result,names=('Column1','Column2','Column3'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.