home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 438947247

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
438947247 MDU6SXNzdWU0Mzg5NDcyNDc= 2933 Stack() & unstack() issues on Multindex 1559890 closed 0     4 2019-04-30T19:47:51Z 2022-04-09T15:23:28Z 2022-04-09T15:23:28Z NONE      

I would like to reshape the DataArray by one level in the Multindex, and I thought the stack()/unstack() should be the solution.

Make a DataArray with Multindex: python import pandas as pd arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo']), np.array(['one', 'two', 'one', 'two', 'one', 'two'])] da = pd.DataFrame(np.random.randn(6, 4)).to_xarray().to_array() da.coords['index'] = pd.MultiIndex.from_arrays(arrays, names=['first', 'second']) da <xarray.DataArray (variable: 4, index: 6)> array([[ 0.379189, 1.082292, -2.073478, -0.84626 , -1.529927, -0.837407], [-0.267983, -0.2516 , -1.016653, -0.085762, -0.058382, -0.667891], [-0.013488, -0.855332, -0.038072, -0.385211, -2.149742, -0.304361], [ 1.749561, -0.606031, 1.914146, 1.6292 , -0.515519, 1.996283]]) Coordinates: * index (index) MultiIndex - first (index) object 'bar' 'bar' 'baz' 'baz' 'foo' 'foo' - second (index) object 'one' 'two' 'one' 'two' 'one' 'two' * variable (variable) int32 0 1 2 3

Stack problem:

I want a dimension merges into another one: python da.stack({'index':['variable']}) ValueError: cannot create a new dimension with the same name as an existing dimension

Unstack problem:

Unstacking by the whole Multindex worked: python da.unstack('index') ``` <xarray.DataArray (variable: 4, first: 3, second: 2)> array([[[ 0.379189, 1.082292], [-2.073478, -0.84626 ], [-1.529927, -0.837407]],

   [[-0.267983, -0.2516  ],
    [-1.016653, -0.085762],
    [-0.058382, -0.667891]],

   [[-0.013488, -0.855332],
    [-0.038072, -0.385211],
    [-2.149742, -0.304361]],

   [[ 1.749561, -0.606031],
    [ 1.914146,  1.6292  ],
    [-0.515519,  1.996283]]])

Coordinates: * variable (variable) int32 0 1 2 3 * first (first) object 'bar' 'baz' 'foo' * second (second) object 'one' 'two' But unstacking by a specified level failed:python da.unstack('first') ValueError: Dataset does not contain the dimensions: ['first'] ```

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/2933/reactions",
    "total_count": 1,
    "+1": 1,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 0 rows from issues_id in issues_labels
  • 4 rows from issue in issue_comments
Powered by Datasette · Queries took 153.009ms · About: xarray-datasette