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

Saving a structured numpy array with scipy.io.savemat then reloading it changes original dtype to 'O'?

I am saving a structured numpy array of the to a mat file using scipy.io.savemat this way: sio.savemat(filename, { ‘myStructuredArray’: myStructuredArray}, appendmat=True ) then I reload it this way: mat = sio.loadmat( filename, squeeze_me=True)…
Baba
  • 475
  • 6
  • 19
0
votes
1 answer

How to add or construct nested numpy structured array from existing structures

In general, my question is on the possible ways for creating/appending to nested structured arrays. Specifically where the dtype structure is known, but the size in the nested elements weren't pre-defined. I have tried different things with the…
001001
  • 530
  • 1
  • 4
  • 13
0
votes
1 answer

Is there a simple way to remove "padding" fields from numpy.dtype.descr?

Context Since numpy version 1.16, if you access multiple fields of a structured array, the dtype of the resulting array will have the same item size as the original one, leading to extra "padding": The new behavior as of Numpy 1.16 leads to extra…
mapf
  • 1,906
  • 1
  • 14
  • 40
0
votes
1 answer

Finding matching subset of "row" in a numpy structured array

I have data stored in a NumPy structured array where part of the information identifies various cases. I would like to find the row that matches a given case. E.g., let's say I'm storing the name of a building, room number, and the number of chairs…
M.A.R.
  • 37
  • 5
0
votes
1 answer

Numpy savetxt Structured Array ValueError: fmt has wrong number of % formats

import numpy as np row_a = ['0.01722497', '', '0.09496404', '0.03654174', '0.03624997', '0.01583785', '0.02002064', '0.13934049', '0.0405615', '0.05686177', '', '0.08495372', '0.00619173', '0.00515492', '0.01053369', '0.06576333'] row_b =…
gargoylebident
  • 373
  • 1
  • 2
  • 12
0
votes
1 answer

Creating own type from numpy.dtype for structured array. What is the cleanest way to obtain this?

I would like to derive an own class from numpy.dtype like this: import numpy as np class A(np.dtype): def __new__(cls): cls.fields = [("field1", np.int32), ("field2"), np.int64)] However, numpy won't let me do this: type 'numpy.dtype'…
0
votes
0 answers

Recommended use of Numpy's structured arrays

I want to save 1D arrays into each entry of another 1D array. Essentially I have a list of grid points (1D array) for which each I would like to save a 1D array containing information specific to the grid point. (Imagine for each of a list of cities…
0
votes
1 answer

Pandas dataframe.to_numpy() with specific dtypes

I have a dataframe with two columns: In[] df.head() Out[] specific_death months_survival 0 False 179 1 False 127 2 False 67 3 True …
0
votes
1 answer

Structured numpy array with 2 different data types

I imported a csv file into a numpy array which I need to convert to a structured array with only the first column as dtype string and all the other 47 columns as float. How do I define data type for the other 47 columns in a single operation? Do I…
Neyls
  • 87
  • 1
  • 7
0
votes
1 answer

loadtxt to structured array and adding one column with value from filename

i`m new to python and nupmy. I have to import some Data from txt-File and insert it to a postgresql database. I read the data this way: type_definitions = ([('StationID', 'S4'), ('East', np.float), ('North', np.float), ('Height',…
randn
  • 5
  • 3
0
votes
1 answer

numpy structured array sorting by multiple columns

A minimal numpy structured array generator: import numpy as np index = np.arange(4) A = np.stack((np.sin(index), np.cos(index)),axis=1) B = np.eye(4).astype(int) C = np.array([1, 0, 1, 0], dtype=bool) goodies = [(a, b, c, d) for a, b, c, d in…
uhoh
  • 3,713
  • 6
  • 42
  • 95
0
votes
1 answer

Is there a way to declare a structured array that has a string field of arbitrary lengh?

This has bugged me for some time now, but I haven't really found a satisfying solution. If you declare a structured array with a field that contains strings, how can you set the dtype of that field to something so that you don't have to worry about…
mapf
  • 1,906
  • 1
  • 14
  • 40
0
votes
1 answer

Structured numpy array within a multidimensional array

Imagine a numpy array of N x M dimension. In each cell, it contains a structured array with X elements, each containing an x_label. I would like to access a specific x_label so it returns a N x M array only containing the value of the label of…
0
votes
3 answers

Create Structured Numpy Array with Different Types

I have the following unstructured data (read from a csv). data = [[b'id' b'datetime' b'anomaly_length' b'affected_sensors' b'reason'] [b'1' b'2019-12-20 08:09' b'26' b'all' b'Open Windows'] [b'1' b'2019-12-20 08:10' b'26' b'all' b'Open Windows'] …
wake-0
  • 3,918
  • 5
  • 28
  • 45
0
votes
1 answer

How to build a numpy structured array with 2 int columns and 3 float columns?

I have data which is a list of 5-tuples. The first two are integer indices i, j and the next three are floats xyz. data = [(1, 2, 3.141, 1.414, 2.718), (3, 4, 1.111, 2.222, 3.333), (0, 0, 0.000, 0.000, 0.000)] I have heard that I…
uhoh
  • 3,713
  • 6
  • 42
  • 95