Questions tagged [structured-array]

Numpy structured arrays (aka "Record arrays") allow for inhomogenous datatypes (structs/records) to be stored in a single numpy array

Numpy structured arrays (aka "Record arrays") allow for inhomogenous datatypes (structs/records) to be stored in a single numpy array.

150 questions
1
vote
1 answer

More pythonic (shorter/efficient) way of filling a structured array with the content of a structured string than this?

I need to put a formated string into a structured array (the string is a JSON formated 2D table, where all columns are objects). Right now, I do so: import json import numpy json_string = '{"SYM": ["this_string","this_string","this_string"],"DATE":…
user189035
  • 5,589
  • 13
  • 52
  • 112
1
vote
3 answers

Use numpy structured array instead of dict to save space and keep speed

Are numpy structured arrays an alternative to Python dict? I would like to save memory and I cannot affort much of a performance decline. In my case, the keys are str and the values are int. Can you give a quick conversion line in case they actually…
1
vote
1 answer

Broken structured to unstructured numpy array conversion in 1.16.0

I want to convert NumPy structured array with columns of the same (np.float) type to unstructured array in Numpy 1.16.0. Previously I did it like this: array = np.ones((100,), dtype=[('user', np.object), ('item', np.float), ('value',…
1
vote
2 answers

Structured arrays: Do operations on views result in scattered arrays?

I have a numpy structured array a and create a view b on it: import numpy as np a = np.zeros(3, dtype={'names':['A','B','C'], 'formats':['int','int','float']}) b = a[['A', 'C']] The descr component of the data type of b indicates that the data are…
Samufi
  • 2,465
  • 3
  • 19
  • 43
1
vote
1 answer

How to access specific entries in sub-array of a structured array

Using the example given here: dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))]) x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt) How can I access only the grades at say position 0 in the 'grades'…
mapf
  • 1,906
  • 1
  • 14
  • 40
1
vote
1 answer

How to save a list of python dictionaries as an array of matlab structured arrays?

I am trying to create a file to be read in a matlab enviroment. The structure in matlab looks like this trx(1) = x: [1×1500 double] y: [1×1500 double] a: [1×1500 double] b: [1×1500 double] theta:…
1
vote
1 answer

Use structured array to name axis in numpy array

I must be making some sort of really trivial mistake, but I'm trying to create a structured array with names for a single axis, e.g., I have an array data with shape (2, 3, 4), and I want to name the first axis such that I can access data['a'] and…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
1
vote
1 answer

Multiplying ndarray with scalar: TypeError: invalid type promotion

I'm trying to multiply every column in an ndarray by a scalar. When I try to do this, I get the error TypeError: invalid type promotion. I've tried using array.astype(float), but this gives all NaNs. array = np.genfromtxt("file.csv", dtype=float,…
gilmour
  • 23
  • 1
  • 4
1
vote
1 answer

Change how structured arrays and recarrays are printed

Numpy summarizes large arrays, which is convenient when working in an interactive session. Unfortunately, structured arrays and recarrays are not summarized very well by default. Is there a way to change this? By default, the full array is…
user2699
  • 2,927
  • 14
  • 31
1
vote
2 answers

Adding a data column to a numpy rec array with only one row

I need to add a column of data to a numpy rec array. I have seen many answers floating around here, but they do not seem to work for a rec array that only contains one row... Let's say I have a rec array x: >>> x = np.rec.array([1, 2, 3]) >>>…
pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
1
vote
2 answers

View of numpy structured array with offsets

I have the following numpy structured array: In [250]: x Out[250]: array([(22, 2, -1000000000, 2000), (22, 2, 400, 2000), (22, 2, 804846, 2000), (44, 2, 800, 4000), (55, 5, 900, 5000), (55, 5, 1000, 5000), (55, 5, 8900, 5000), (55, 5,…
snowleopard
  • 717
  • 8
  • 19
1
vote
1 answer

Finding the index of a header in a structured array for a part of the header name

In my list there are strings such as A_list= ['mass_32_', 'mass_40_', 'mass_28_'] I want to find the index of an element from an array of just the numbers. For example masses=[32,28] I want to find the index of the ints in masses that correspond to…
sgartz
  • 13
  • 2
1
vote
1 answer

Memory-friendly way to add a field to a structured ndarray — without duplicating data?

To add a field to a structured numpy array, it is quite simply to create a new array with a new dtype, copy over the old fields, and add the new field. However, I need to do this for an array that takes a lot of memory, and I would rather not…
gerrit
  • 24,025
  • 17
  • 97
  • 170
1
vote
0 answers

Reading a binary file using np.fromfile()

I have a binary file that has numerous sections. Each section has its own pattern (i.e. the placement of integers, floats, and strings). The pattern of each section is known. However, the number of times that pattern occurs within the section is…
1
vote
1 answer

Python: The values of a record array cannot be set correctly in numpy

I am trying to create a record array with specific values. However, I noticed that the values of the record array cannot be set correctly. The record array has three fields: "startstate" (a scalar), "action" (a 4x1 array), and "transition" (a 4x2…
user3821012
  • 1,291
  • 2
  • 16
  • 27