Questions tagged [recarray]

A *rec*ord *array* in the python package numpy - think of as a table with column names.

A record array is particular to the numpy package. It is essentially an ndarray that can be accessed by associative indices.

Ex: create an array with two fields, 'x' (float) and 'y' (int):

>>> ra = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)])
>>> ra
array([(1.0, 2), (3.0, 4)],
      dtype=[('x', '<f8'), ('y', '<i4')])
>>> ra['x']
array([ 1.,  3.])
>>> ra[0]['y']
2

See the recarray help page and cookbook.

103 questions
57
votes
5 answers

Convert structured array to regular NumPy array

The answer will be very obvious I think, but I don't see it at the moment. How can I convert a record array back to a regular ndarray? Suppose I have following simple structured array: x = np.array([(1.0, 4.0,), (2.0, -1.0)], dtype=[('f0', '
joris
  • 133,120
  • 36
  • 247
  • 202
36
votes
5 answers

Sorting a python array/recarray by column

I have a fairly simple question about how to sort an entire array/recarray by a given column. For example, given the array: import numpy as np data = np.array([[5,2], [4,1], [3,6]]) I would like to sort data by the first column to…
mike
  • 22,931
  • 31
  • 77
  • 100
29
votes
1 answer

Passing a structured numpy array with strings to a cython function

I am attempting to create a function in cython that accepts a numpy structured array or record array by defining a cython struct type. Suppose I have the data: a = np.recarray(3, dtype=[('a', np.float32), ('b', np.int32), ('c', '|S5'), ('d',…
JoshAdel
  • 66,734
  • 27
  • 141
  • 140
18
votes
1 answer

numpy recarray strings of variable length

Is it possible to initialise a numpy recarray that will hold strings, without knowing the length of the strings beforehand? As a (contrived) example: mydf = np.empty( (numrows,), dtype=[ ('file_name','STRING'), ('file_size_MB',float) ] ) The…
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
15
votes
3 answers

Adding a field to a structured numpy array (2)

I know there was already a question about this topic (cleanest way to add a field to a structured numpy array), see Adding a field to a structured numpy array but I have a question about the answer given there ... If you're using numpy 1.3,…
joris
  • 133,120
  • 36
  • 247
  • 202
14
votes
7 answers

numpy: How to add a column to an existing structured array?

I have a starting array such as: [(1, [-112.01268501699997, 40.64249414272372]) (2, [-111.86145708699996, 40.4945008710162])] The first column is an int and the second is a list of floats. I need to add a str column called 'USNG'. I then create a…
code base 5000
  • 3,812
  • 13
  • 44
  • 73
12
votes
3 answers

Subclassing numpy ndarray problem

I would like to subclass numpy ndarray. However, I cannot change the array. Why self = ... does not change the array? Thanks. import numpy as np class Data(np.ndarray): def __new__(cls, inputarr): obj = np.asarray(inputarr).view(cls) …
riza
  • 16,274
  • 7
  • 29
  • 29
11
votes
3 answers

Stacking numpy recarrays without losing their recarrayness

Suppose I make two recarrays with the same dtype and stack them: >>> import numpy as np >>> dt = [('foo', int), ('bar', float)] >>> a = np.empty(2, dtype=dt).view(np.recarray) >>> b = np.empty(3, dtype=dt).view(np.recarray) >>> c =…
Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88
10
votes
1 answer

Get recarray attributes/columns python

I'm trying to retrieve the column titles of a recarray, and running into considerable trouble. If I read in a .csv file using pylab's csv2rec function, I am able to access column titles in the following manner: from pylab import csv2rec x =…
mike
  • 22,931
  • 31
  • 77
  • 100
9
votes
2 answers

Select Rows from Numpy Rec Array

I have a Numpy rec array from which I would like to do some quick queries similar to SQL: SELECT * where array['phase'] == "P". I would like to get a Record Array as output with each row corresponding to a row from the original array that met the…
Rishi
  • 3,538
  • 5
  • 29
  • 40
7
votes
2 answers

Normalize/Standardize a numpy recarray

I wonder what the best way of normalizing/standardizing a numpy recarray is. To make it clear, I'm not talking about a mathematical matrix, but a record array that also has e.g. textual columns (such as labels). a = np.genfromtxt("iris.csv",…
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
7
votes
1 answer

Access a Numpy Recarray via the C-API

If we have a Numpy recarray: x = np.array([(1.,2.)], dtype=np.dtype([('a','
Joel Vroom
  • 1,611
  • 1
  • 16
  • 30
7
votes
1 answer

How to make a numpy recarray with datatypes (datetime,float)?

Let's say I have some simple data y = [[datetime.datetime( 2012,1,1,1,1), 2.1], [datetime.datetime( 2012,1,1,1,2), -3.1], [datetime.datetime( 2012,1,1,1,3), 0.1]] and I want a numpy record array corresponding to it. It would seem I…
Brian B
  • 1,410
  • 1
  • 16
  • 30
6
votes
1 answer

Python Numpy Structured Array (recarray) assigning values into slices

The following example shows what I want to do: >>> test rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)], dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy',…
joon
  • 63
  • 1
  • 3
6
votes
1 answer

Python Numpy recarray sort bidirectional

I have a structured numpy array, and I am sorting it by a order. It works pretty fine but in just one direction! Descending: sort(myStructuredArray,order=my_order)[::-1] and Ascending: sort(myStructuredArray,order=my_order) The order my_order is…
user2209128
  • 95
  • 1
  • 4
1
2 3 4 5 6 7