Right now (0.18.1), I can construct a DataFrame from a "1D" structured array. The NumPy structure field names are used as the Pandas column names.
But I cannot construct a Panel from a structured array:
arr = np.zeros((3,5), [('x',float), ('y',bool)])
pd.Panel(arr) # error
ValueError: The number of dimensions required is 3, but the number of dimensions of the ndarray given was 2
One "obvious" way is slow:
pd.Panel(dict(enumerate(pd.DataFrame(a) for a in arr)))
And an efficient way is verbose:
dtyp = np.array(arr.dtype.names)
dim = arr.shape[0], arr.shape[1], dtyp.shape[0]
pd.Panel(pd.DataFrame(arr.ravel()).values.reshape(dim), minor_axis=dtyp)
Note that both of the above ways "promote" all values to a single dtype, which in my case (float + bool) becomes object. That's really bad--I need to preserve the original dtypes so that if I do panel[0].dtypes
matches arr[0].dtype
. I created #14512 for that problem.
Background for this issue is here: http://stackoverflow.com/questions/40259034/construct-pandas-panel-from-2d-structured-numpy-array
I'm using Pandas 0.18.1.
Comment From: shoyer
Take a look at xarray: http://xarray.pydata.org/en/stable/pandas.html#transitioning-from-pandas-panel-to-xarray
There are plans to eventually deprecate Panel.
Comment From: jzwinck
@shoyer Thanks for the link. I looked, but I don't entirely follow. It sounds like you're saying that Pandas Panel will be deprecated but the replacement will be an entirely different library that has to be installed separately. Am I understanding that correctly?
Comment From: jorisvandenbossche
@jzwinck That is indeed correct. Pandas will not directly replace Panels with something else if they are deprecated, as we believe that xarray
provides a better and actively developed alternative. The mapping in functionality will not be 1:1 though.
I will close this issue, as we will not probably actively work on enhancements to panels. You can always make a helper function for the 'verbose' way yourself.