Guido
Guido

Reputation: 483

3D numpy recarray

# pre-allocate data cube
cube = np.empty((len(time_ix), len(data_ix), len(id_ix)))
cube[:] = np.NaN
# filling of cube
tix=3
idx=5
data = cube[tix,:,idx]

Data is describing the values of 20 columns roughly at that day for that id

I am creating a cube to slice better my data afterwards, unfortunately by using such statement I can fill my 2nd dimension with the data having data type only float64 that turned to be quite expensive in terms of storage. I didn't find a way to declare the above mentioned cube as a rec array, in such a way that I could fit into the data_ix dimension heterogeneous data types.

Alternatively, is there a way to represent a 3d array (cube) with 2 indexes to easily slic (time and id) to get the according dataset with pandas dataframes?

Upvotes: 1

Views: 98

Answers (1)

piRSquared
piRSquared

Reputation: 294218

IIUC:

Use a 2-D rec array

x = np.array([[(1.0, 2), (3.0, 4)], [(0.0, 1), (7.0, 5)]], dtype=[('x', '<f8'), ('y', '<i8')])

Then

x['x']

array([[1., 3.],
       [0., 7.]])

Gives a float array while

x['y']

array([[2, 4],
       [1, 5]], dtype=int64)

gives an integer array

Upvotes: 0

Related Questions