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

numpy Structured arrays append and remove records

let's say we have this structured array : x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]) how to delete the first row :('Rex', 9, 81.0) ? and how add another row ??
NAS
  • 188
  • 11
1
vote
1 answer

Creating a structured array with mixed data types, but cannot create boolean type

So I'm trying to make the 4th column in this one-dimensional array of the boolean data type. I've read that either '?' or 'b' are supposed to work, but neither do. The error I keep getting is shown below after the code I'm using: import numpy as…
1
vote
1 answer

How to change the data types of specific data type records in a NumPy structured array from a list of corresponding indices? (Python)

I have NumPy structured array data: data_tup = [tuple(ele) for ele in array] data = np.array( data_tup, dtype = [("field_1", "
Sohaib
  • 23
  • 3
1
vote
1 answer

h5py: assigning or broadcasting to 2×2 column in a structured array

I have record array with 2×2 fixed-size item, with 10 rows; thus the column is 10×2x2. I would like to assign a constant to the whole column. Numpy array will broadcast scalar value correctly, but this does not work in h5py. import numpy as…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
1
vote
2 answers

Sort a structured Numpy array based on a single column while preserving order in other columns

I want to sort the following input by 1st column: a = np.array([(2, 1), (1, 2), (2, 3)], dtype=[('c1', int), ('c2', int)]) [[2, 1], [1, 2], [2, 3]] I've tried a.sort(order='c1') followed by print(a[::-1]. Expected output (2nd column order is…
gargoylebident
  • 373
  • 1
  • 2
  • 12
1
vote
1 answer

Numpy repack_fields with range or a list allocates memory

Issue: I am trying to repack a subset of rows and fields from a large numpy structured array. When I use a slice, I am able to use repack_fields, but when I use a range I am not. Calling range before repack_fields appears to be allocating all of the…
jmlarson
  • 837
  • 8
  • 31
1
vote
1 answer

Quicker Method to Group Numpy Array Elements Based on Second Numpy Array

There are 2 NumPy arrays groups and selectors, where selectors is an array containing integers that needs to be grouped import numpy as np np.random.seed(0) selectors = np.random.randint(0, 300, 5) # [172 47 117 192 251] groups is a structured…
Athena Wisdom
  • 6,101
  • 9
  • 36
  • 60
1
vote
1 answer

Record data type for heterogeneous Numpy arrays

I am working with numpy and to create a heterogeneous array it is necesary to use dtype() function. After read some python doc about this function I think I know how it works; t = np.dtype([('name', str),('edad',int)]) <-- This tells to python that…
1
vote
1 answer

Numpy: how to extract rows of numpy array between multiple pairs of values?

I am relatively new to Python and I am currently facing some problems in implementing a conceptually simple algorithm in an efficient way. I have been able to do it in pandas (but it is quite slow to execute). I have a ndarray composed by n rows and…
Luigi
  • 13
  • 3
1
vote
1 answer

Different behaviour of indexing and slicing in numpy structured arrays

Suppose you have a structured array a: import numpy as np a = np.array([1, 2, 3, 4, 5, 6], dtype=[('val', 'i4')]) print(a) [(1,) (2,) (3,) (4,) (5,) (6,)] Now, if I would like to change one of the entries to a different value, the following two…
mapf
  • 1,906
  • 1
  • 14
  • 40
1
vote
1 answer

Saving individual columns of a structured array with numpy.savetxt: ValueError: fmt has wrong number of % formats

I am a bit puzzled, because this worked previously when I run the code 1-2 years ago. I have a large structured numpy array with different data types and column names. I can save it with numpy.savetxt() if I provide a format string (fmt) that…
DAH
  • 11
  • 1
1
vote
1 answer

How to *actually* delete a column from numpy structured array (so that it won't show up in binary file)

I have a structured array which is loaded from a binary file. In [85]: dx = np.dtype([('op', '
1
vote
1 answer

How to efficiently write raw bytes to numpy array data in python 3

While migrating some old python 2 code to python 3, I ran into some problems populating structured numpy arrays from bytes objects. I have a parser that defines a specific dtype for each type of data structure I might encounter. Since, in general,…
1
vote
1 answer

Given that you added a new field to a 1-d slice of a structured array, why can you not set the entry of the new field to a list?

The title may be a little bit confusing, so I hope I can make it clearer with the help of an example. Image I have a little helper function that adds new fields to already existing structured arrays: import numpy as np def add_field(a, *descr): …
mapf
  • 1,906
  • 1
  • 14
  • 40
1
vote
1 answer

How can I assign slices of one structured Numpy array to another?

I have two numpy structured arrays arr1, arr2. arr1 has fields ['f1','f2','f3']. arr2 has fields ['f1','f2','f3','f4']. I.e.: arr1 = [[f1_1_1, f2_1_1, f3_1_1 ], arr2 = [[f1_2_1, f2_2_1, f3_2_1, f4_2_1 ], [f1_1_2, f2_1_2, f3_1_2…