Skip to content

ENH: Construct Panel from 2D structured array #14511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jzwinck opened this issue Oct 27, 2016 · 3 comments
Closed

ENH: Construct Panel from 2D structured array #14511

jzwinck opened this issue Oct 27, 2016 · 3 comments

Comments

@jzwinck
Copy link
Contributor

jzwinck commented Oct 27, 2016

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.

@shoyer
Copy link
Member

shoyer commented Oct 27, 2016

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.

@jzwinck
Copy link
Contributor Author

jzwinck commented Oct 27, 2016

@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?

@jreback jreback added the Deprecate Functionality to remove in pandas label Oct 27, 2016
@jorisvandenbossche
Copy link
Member

@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.

@jorisvandenbossche jorisvandenbossche removed the Deprecate Functionality to remove in pandas label Oct 27, 2016
@jorisvandenbossche jorisvandenbossche added this to the No action milestone Oct 27, 2016
@jreback jreback modified the milestones: No action, won't fix Jul 11, 2017
@TomAugspurger TomAugspurger modified the milestones: won't fix, No action Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants