I am having some trouble understanding why the following does not work:
np.dtype(dict(names="10", formats=np.float64))
I have been struggling with this because I would like to get the recfunctions function in numpy to work, but due to issues with the numpy.dtype, I haven't been successful. This is the error I am receiving at the moment:
dtype = np.dtype(dict(names=names, formats=formats))
ValueError: all items in the dictionary must have the same length.
I want to get a data structure that will contain a type of record array with multiple columns of data within each assigned field - similar to a dictionary where each value is a 2d array or several columns of data. Typically the data may end up being ~6 columns, ~2000 rows for each key or record, with ~200 records.
Here is what I have tried in a complete script: (although still giving the same error)
import numpy as np
from numpy.lib import recfunctions
# Just function to make random data
def make_data(i, j):
# some arbitrary function to show that the number of columns may change, but rows stay the same length
if i%3==0:
data = np.array([[i for i in range(0,1150)]*t for t in range(0,3)])
else:
data = np.array([[i for i in range(0,1150)]*t for t in range(0,6)])
return data
def data_struct(low_ij, high_ij):
"""
Data Structure to contain several columns of data for different combined values between "low ij" and "high ij"
Key: "(i, j)"
Value: numpy ndarray (multidimensional)
"""
for i in range(0,low_ij+1):
for j in range(0,high_ij+1):
# Get rid of some of the combinations
# (unimportant)
if(i<low_ij and j<low_ij):
break
elif(i<j):
break
# Combinations of interest to create structure
else:
names = str(i)+str(j)
formats = np.float64
data = np.array(make_data(i, j))
try:
data_struct = recfunctions.append_fields(base=data_struct, names=names, data=data, dtypes=formats)
# First loop will assign data_struct using this exception,
# then proceed to use the try statement to add on the rest of the data
except UnboundLocalError:
dtype = np.dtype(dict(names=names, formats=formats))
data_struct = np.array(data, dtype=dtype)
return data_struct