Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyTables批处理获取和更新_Python_Hdf5_Pytables - Fatal编程技术网

Python PyTables批处理获取和更新

Python PyTables批处理获取和更新,python,hdf5,pytables,Python,Hdf5,Pytables,我将每日股票数据作为使用PyTables创建的HDF5文件。我希望获得一组行,将其作为数组处理,然后使用PyTables将其写回磁盘(更新行)。我想不出一个干净的方法。你能告诉我实现这一目标的最佳方法是什么吗 我的数据: Symbol, date, price, var1, var2 abcd, 1, 2.5, 12, 12.5 abcd, 2, 2.6, 11, 10.2 abcd, 3, 2.45, 11, 10.3 defg, 1,12.34, 19.1, 18.1 defg, 2, 11

我将每日股票数据作为使用PyTables创建的HDF5文件。我希望获得一组行,将其作为数组处理,然后使用PyTables将其写回磁盘(更新行)。我想不出一个干净的方法。你能告诉我实现这一目标的最佳方法是什么吗

我的数据:

Symbol, date, price, var1, var2
abcd, 1, 2.5, 12, 12.5
abcd, 2, 2.6, 11, 10.2
abcd, 3, 2.45, 11, 10.3
defg, 1,12.34, 19.1, 18.1
defg, 2, 11.90, 19.5, 18.2
defg, 3, 11.75, 21, 20.9
defg, 4, 11.74, 22.2, 21.4
我想将每个符号对应的行作为数组读取,进行一些处理并更新字段var1和var2。我预先知道所有的符号,这样我就可以在它们之间循环。我试过这样的方法:

rows_array = [row.fetch_all_fields() for row in table.where('Symbol == "abcd"')]
我想将rows\u数组传递给另一个函数,该函数将计算var1和var2的值,并为每个记录更新它。请注意,var1和var2类似于移动平均值,因此我无法在迭代器中计算它们,因此需要将整个行集作为一个数组

在我使用rows_数组计算了我需要的任何内容之后,我不确定如何将其写回数据,即使用新的计算值更新行。更新整个表时,我使用以下方法:

 table.cols.var1[:] = calc_something(rows_array)
然而,当我只想更新表的一部分时,我并不是最好的方法。我想我可以重新运行“where”条件,然后根据我的计算更新每一行,但重新扫描表似乎是浪费时间

谢谢你的建议

谢谢,
-e

如果我理解得很好,下一个应该做你想做的事:

condition = 'Symbol == "abcd"'
indices = table.getWhereList(condition)  # get indices
rows_array = table[indices]  # get values
new_rows = compute(rows_array)   # compute new values
table[indices] = new_rows  # update the indices with new values

希望这有帮助

谢谢,弗朗西斯。这很有效。我猜第二个列表将再次扫描表?我修改了代码,所以我只需要先获取索引,然后使用索引读取表值,然后再次使用索引进行更新。哦,当然。我已经编辑了我以前的答案,以遵循您的建议。我不想在行数组中循环,而是想直接获得一个列,我尝试了以下方法:price=table.cols.price[index]。我发现以下错误:getitem“%s”中的文件“/Library/Python/2.6/site packages/tables/table.py”,第3063行“%s”键类型在此上下文中无效“%key”)类型错误:“[0 1 2 3 4 5 6 7 8 9 10]”键类型在此上下文中无效关于如何从索引中提取整个列的任何建议,在列级别还不支持花式索引。但是你可以一直这样做:
price=table[index]['price']
,这也非常有效。