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
19
votes
1 answer

Why does creating this memoryview raise a ValueError only when assigning to a variable?

Pythons memoryview does not support datetime64 or timedelta. Ok. But when I try to create a memoryview of a structured array that includes a datetime64 or timedelta, it appears to work... unless I assign it to a variable! In [19]:…
gerrit
  • 24,025
  • 17
  • 97
  • 170
15
votes
1 answer

Structured 2D Numpy Array: setting column and row names

I'm trying to find a nice way to take a 2d numpy array and attach column and row names as a structured array. For example: import numpy as np column_names = ['a', 'b', 'c'] row_names = ['1', '2', '3'] matrix = np.reshape((1, 2, 3, 4, 5, 6, 7,…
freebie
  • 2,161
  • 2
  • 19
  • 36
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
11
votes
2 answers

Creating a structured array from a list

I have a simple list of elements and I'm trying to make a structured array out of it. This naive approach fails: y = np.array([1,2,3], dtype=[('y', float)]) TypeError: expected an object with a buffer interface Putting each element in a tuple…
Jérôme
  • 13,328
  • 7
  • 56
  • 106
8
votes
3 answers

No binary operators for structured arrays in Numpy?

Okay, so after going through the tutorials on numpy's structured arrays I am able to create some simple examples: from numpy import array, ones names=['scalar', '1d-array', '2d-array'] formats=['float64', '(3,)float64', '(2,2)float64'] my_dtype =…
user2789194
  • 135
  • 4
7
votes
1 answer

numpy: how to fill multiple fields in a structured array at once

Very simple question: I have a structured array with multiple columns and I'd like to fill only some of them (but more than one) with another preexisting array. This is what I'm trying: strc = np.zeros(4, dtype=[('x', int), ('y', int), ('z',…
Federico Barabas
  • 659
  • 8
  • 21
6
votes
1 answer

How can I mask elements of a record array in Numpy?

I understand how to create a masked array, and I would like to use masking in a record array so that I can access this data using named attributes. The masking seems to be "lost" when I create a record array from a masked array: >>> data =…
Nate Reed
  • 6,761
  • 12
  • 53
  • 67
6
votes
2 answers

Changing numpy structured array dtype names and formats

I am doing some work with structured arrays in numpy (that I will eventually convert to a pandas dataframe). Now, I generate this structured array by reading in some data (actually memmapping some data) and then filtering it by user specified…
Andrew
  • 693
  • 6
  • 19
5
votes
3 answers

Save structured numpy array using np.savetxt with header

I have a structure array in the form of output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)]) Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each…
somebodyzh
  • 51
  • 1
  • 3
5
votes
1 answer

Splitting numpy array field values that are matrices into column vectors

I have the following numpy structured array: x = np.array([(22, 2, -1000000000.0, [1000,2000.0]), (22, 2, 400.0, [1000,2000.0])], dtype=[('f1', '
snowleopard
  • 717
  • 8
  • 19
4
votes
2 answers

Converting numpy array to structured array

Let's say I have the following array: arr = np.array([[1,2], [3,4]], dtype='u1') and I want to convert it into a structured array like this one: strarr = np.array([(1,2), (3,4)], dtype=[('a', 'u1'), ('b', 'u1')]) If I just try arr.astype([('a',…
uzumaki
  • 1,743
  • 17
  • 32
4
votes
3 answers

Completely nesting NumPy structured scalars

In the NumPy docs and in other StackOverflow questions, nested NumPy structured scalars are mentioned. Everywhere I've seen this, they seem to describe a nested structured scalar as a scalar which contains another scalar (obviously), but the inner…
Sam Ragusa
  • 422
  • 3
  • 13
4
votes
1 answer

Unexpected behaviour with numpy advanced slicing in named arrays

When using numpy named arrays I observe a different behaviour in the following two cases: case: first using an index array for advanced slicing and then selecting a subarray by name case: first selecting a subarray by name and then using an index…
murban
  • 47
  • 1
  • 5
4
votes
1 answer

Truly recursive `tolist()` for NumPy structured arrays

From what I understand, the recommended way to convert a NumPy array into a native Python list is to use ndarray.tolist. Alas, this doesn't seem to work recursively when using structured arrays. Indeed, some ndarray objects are being referenced in…
ChristopherC
  • 1,635
  • 16
  • 31
4
votes
0 answers

pandas DataFrame very slow when initializing with numpy structured array

I have a numpy structured array that has integers and floats, I use it to initialize a pandas DataFrame: In [497]: x = np.ones(100000000, dtype=[('f0', '
snowleopard
  • 717
  • 8
  • 19
1
2 3
9 10