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.

130 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
  • 17,590
  • 12
  • 72
  • 135
12
votes
5 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
  • 2,892
  • 8
  • 37
  • 62
10
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
  • 9,435
  • 2
  • 42
  • 73
8
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
  • 1,689
  • 2
  • 15
  • 27
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
  • 599
  • 6
  • 20
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
  • 5,843
  • 9
  • 47
  • 62
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
  • 647
  • 7
  • 17
5
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
  • 613
  • 5
  • 18
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
  • 372
  • 2
  • 12
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
2 answers

comparing numpy structured arrays

The quick problem I would like to be able to compare specific dtype fields from two numpy structured arrays that are guaranteed to have the same dtype. I would like to do this in a way that allows the fields we are comparing to be different each…
Andrew
  • 613
  • 5
  • 18
4
votes
1 answer

numpy fromfile and structured arrays

I'm trying to use numpy.fromfile to read a structured array (file header) by passing in a user defined data-type. For some reason, my structured array elements are coming back as 2-d Arrays instead of flat 1D arrays: headerfmt='20i,20f,a80' dt =…
mgilson
  • 264,617
  • 51
  • 541
  • 636
3
votes
1 answer

How can I use Numba "@vectorize" ufunc with a structured Numpy array?

I'm unable to get a vectorized ufunc to run. Regular @njit works fine and the @vectorize documentation suggests that the vectorize decorators are the same as njit. I'm running on Windows 10, if that makes a difference The demo program is as…
1472580
  • 123
  • 1
  • 6
3
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
  • 31
  • 1
  • 2
1
2 3
8 9