pydata / pydata/xarray

convert n-dim array to pandas dataframe

Open
#4,466 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-documentation
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

I just noticed that doing

da = xr.DataArray(np.random.rand(10, 10, 10), dims=('a', 'b', 'c'))
da.to_pandas()

gives

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/local/bin/anaconda3/envs/test_env/lib/python3.8/site-packages/xarray/core/dataarray.py in to_pandas(self)
   2397         try:
-> 2398             constructor = constructors[self.ndim]
   2399         except KeyError:

KeyError: 3

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-22-2c9fae875981> in <module>
----> 1 da.to_pandas()

~/local/bin/anaconda3/envs/test_env/lib/python3.8/site-packages/xarray/core/dataarray.py in to_pandas(self)
   2398             constructor = constructors[self.ndim]
   2399         except KeyError:
-> 2400             raise ValueError(
   2401                 "cannot convert arrays with %s dimensions into "
   2402                 "pandas objects" % self.ndim

ValueError: cannot convert arrays with 3 dimensions into pandas objects

It is worth raising a suggestion (see below)? Or could you even do da.to_dataset(name='da').to_dataframe() under the hood. I feel the output would be what the user expects but I imagine that is not always true

da.to_dataset(name='da').to_dataframe()

Screenshot from 2020-09-26 22-08-23

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DataArray.to_pandas() entry point and compare its current behavior with da.to_dataset(name='da').to_dataframe() for the three-dimensional example. First confirm whether n-dimensional conversion is intended and what output users should expect; done means an agreed behavior implemented and covered for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.