Construct Pandas Panel from 2D structured NumPy array -


i have 2d numpy array of structs:

arr = np.zeros((3,5), [('x',int), ('y',float)]) 

that is:

array([[(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],        [(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],        [(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)]],        dtype=[('x', '<i8'), ('y', '<f8')]) 

i want create pandas panel it. tried obvious:

pd.panel(arr) 

valueerror: number of dimensions required 3, number of dimensions of ndarray given 2

then discovered hideous pile:

pd.panel(dict(enumerate(pd.dataframe(a) in arr))) 

it produces:

<class 'pandas.core.panel.panel'> dimensions: 3 (items) x 5 (major_axis) x 2 (minor_axis) items axis: 0 2 major_axis axis: 0 4 minor_axis axis: x y 

this "works" inefficient , eyesore.

how such panels meant constructed?

edit: submitted issue here: https://github.com/pandas-dev/pandas/issues/14511

you need provide 3-d array corresponding items, major , minor axes of panel object.

# minor axis corresponds dtype names of array initialized zeros dtyp = np.array(arr.dtype.names) # dimensions included  dim = arr.shape[0], arr.shape[1], dtyp.shape[0] # flatten array , reshape according aforementioned dimensions panel = pd.panel(pd.dataframe(arr.ravel()).values.reshape(dim), minor_axis=dtyp) 

gives:

<class 'pandas.core.panel.panel'> dimensions: 3 (items) x 5 (major_axis) x 2 (minor_axis) items axis: 0 2 major_axis axis: 0 4 minor_axis axis: x y 

to convert df, use to_frame method, so:

panel.to_frame() 

image

timings:

image


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -