相当于 NumPy 中的命名元组?
是否可以创建一个行为非常类似于 collections.namedtuple 的 NumPy 对象,从某种意义上说,可以像这样访问元素:
data[1] = 42
data['start date'] = '2011-09-20' # Slight generalization of what is possible with a namedtuple
我尝试使用复杂的数据类型:
>>> data = numpy.empty(shape=tuple(), dtype=[('start date', 'S11'), ('n', int)])
这将创建一个具有某种命名元组的 0 维值类型;它几乎可以工作:
>>> data['start date'] = '2011-09-20'
>>> data
array(('2011-09-20', -3241474627884561860),
dtype=[('start date', '|S11'), ('n', '<i8')])
但是,元素访问不起作用,因为“数组”是0维:
>>> data[0] = '2011-09-20'
Traceback (most recent call last):
File "<ipython-input-19-ed41131430b9>", line 1, in <module>
data[0] = '2011-09-20'
IndexError: 0-d arrays can't be indexed.
是否有一种方法可以使用 NumPy 对象获得上述所需的行为(通过字符串和索引进行项目分配)?
Is it possible to create a NumPy object that behaves very much like a collections.namedtuple, in the sense that elements can be accessed like so:
data[1] = 42
data['start date'] = '2011-09-20' # Slight generalization of what is possible with a namedtuple
I tried to use a complex data type:
>>> data = numpy.empty(shape=tuple(), dtype=[('start date', 'S11'), ('n', int)])
This creates a 0-dimensional value with a kind of namedtuple type; it almost works:
>>> data['start date'] = '2011-09-20'
>>> data
array(('2011-09-20', -3241474627884561860),
dtype=[('start date', '|S11'), ('n', '<i8')])
However, element access does not work, because the "array" is 0-dimensional:
>>> data[0] = '2011-09-20'
Traceback (most recent call last):
File "<ipython-input-19-ed41131430b9>", line 1, in <module>
data[0] = '2011-09-20'
IndexError: 0-d arrays can't be indexed.
Is there a way of obtaining the desired behavior described above (item assignment through both a string and an index) with a NumPy object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 numpy.rec 模块执行类似的操作。您需要的是该模块中的
record
类,但我不知道如何直接创建此类的实例。一种间接方法是首先创建一个带有单个条目的recarray
:如果您知道如何直接创建
record
实例,请告诉我。You can do something like this using the
numpy.rec
module. What you need is therecord
class from this module, but I don't know how to directly create an instance of such a class. One indrect way is to first create arecarray
with a single entry:If you figure out how to create an instance of
record
directly, please let me know.(编辑为 EOL 建议更具体地回答问题。)
创建 0-dim 数组(我也没有找到标量构造函数。)
访问 0-dim 数组中的元素
我认为最简单的是考虑结构化数组(或重新排列)作为元组的列表或数组,索引按选择列的名称和选择行的整数进行索引。
零维数组,元素是元组,即一条记录,更改:不是标量元素,请参见末尾
数组,其中一个元素
一维数组
直接索引到单个记录,与 EOL 的示例相同,我不知道
尝试理解 EOL 的示例是否有效:标量元素和零维数组是不同的
(注意:我无意中在开放的 python 3.2 解释器中输入了示例,所以有一个 b'...')
(edited as EOL's recommended to be more specific in answering the question.)
create 0-dim array (I didn't find a scalar constructor either.)
access element in 0-dim array
I think the easiest is to think of structured arrays (or recarrays) as list or arrays of tuples, and indexing works by name which selects column and by integers which selects rows.
zero dimensional array, element is tuple, i.e. one record, changed: is not a scalar element, see at end
array with one element
1d array
direct indexing into a single record, same as in EOL's example that I didn't know it works
trying to understand EOL's example: scalar element and zero-dimensional array are different
(Note: I typed the example in an open python 3.2 interpreter by accident, so there is a b'...')
好的,我找到了一个解决方案,但我希望看到一个更优雅的解决方案:
创建一个包含单个元素的一维数组并获取该元素。这使得访问元素可以使用字符串和数字索引:
如果有一种方法可以直接构造
data
,而不必先创建一个包含一个元素的数组并提取该元素,那就太好了。因为我不确定可以调用什么 NumPy 构造函数......(没有
numpy.void
的文档字符串)。OK, I found a solution, but I would love to see a more elegant one:
creates a 1-dimensional array with a single element and gets the element. This makes accessing elements work with both strings and numerical indices:
It would be nice if there was a way of directly constructing
data
, without having to first create an array with one element and extracting this element. SinceI'm not sure what NumPy constructor could be called… (there is no docstring for
numpy.void
).这是通过 Pandas 包中的“Series”很好地实现的。
例如来自教程:
我刚刚玩了几天,但看起来它有很多东西可以提供。
This is nicely implemented by "Series" in the Pandas package.
For example from the tutorial:
I've just been playing around with this for a few days, but it looks like it has a lot to offer.