question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document ways to reshape a DataArray

See original GitHub issue

Code Sample, a copy-pastable example if possible

A “Minimal, Complete and Verifiable Example” will make it much easier for maintainers to help you: http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

# Your code here

def xr_reshape(A, dim, newdims, coords):
    """ Reshape DataArray A to convert its dimension dim into sub-dimensions given by
    newdims and the corresponding coords.
    Example: Ar = xr_reshape(A, 'time', ['year', 'month'], [(2017, 2018), np.arange(12)]) """


    # Create a pandas MultiIndex from these labels
    ind = pd.MultiIndex.from_product(coords, names=newdims)

    # Replace the time index in the DataArray by this new index,
    A1 = A.copy()

    A1.coords[dim] = ind

    # Convert multiindex to individual dims using DataArray.unstack().
    # This changes dimension order! The new dimensions are at the end.
    A1 = A1.unstack(dim)

    # Permute to restore dimensions
    i = A.dims.index(dim)
    dims = list(A1.dims)

    for d in newdims[::-1]:
        dims.insert(i, d)

    for d in newdims:
        _ = dims.pop(-1)


    return A1.transpose(*dims)


Problem description

[this should explain why the current behavior is a problem and why the expected output is a better solution.]

It would be great to have the above function as a DataArray’s method.

Expected Output

A reshaped DataArray. In the example in the function comment it would correspond to an array like

In[1] Ar.dims Out[1]: (‘year’, ‘month’, ‘lat’, ‘lon’)

Output of xr.show_versions()

# Paste the output here xr.show_versions() here

INSTALLED VERSIONS

commit: None python: 3.6.3.final.0 python-bits: 64 OS: Linux OS-release: 3.10.0-693.5.2.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 LOCALE: fr_FR.UTF-8 xarray: 0.10.4 pandas: 0.23.0 numpy: 1.13.3 scipy: 0.19.1 netCDF4: 1.3.1 h5netcdf: None h5py: 2.7.0 Nio: None zarr: None bottleneck: 1.2.1 cyordereddict: None dask: 0.15.3 distributed: 1.19.1 matplotlib: 2.1.0 cartopy: 0.16.0 seaborn: 0.8.1 setuptools: 36.5.0.post20170921 pip: 18.0 conda: 4.4.7 pytest: 3.2.1 IPython: 6.1.0 sphinx: 1.6.3

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
shoyercommented, Jun 7, 2019

This can be done with a MultiIndex and unstack(), but yes right now it’s definitely an awkward API.

1reaction
fujiisoupcommented, Sep 18, 2018

Thanks for raising an issue, @dimitryx2017. I like this idea.

We usually have methods rather than function. Any idea about the API?

We usually use a keyword argument. I’m thinking something like Ar.reshape(time=(('year', (2017, 2018)), ('month', np.arange(12))), but it looks a little long…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reshaping and reorganizing data - Xarray
These methods allow you to reorganize your data by changing dimensions, array shape, order of values, or indexes. Reordering dimensions: To reorder ...
Read more >
Reshaping and reorganizing data — xarray 0.8.0 documentation
This method broadcasts all data variables in the dataset against each other, then concatenates them along a new dimension into a new array...
Read more >
Reshaping and reorganizing data - xarray - Read the Docs
This method broadcasts all data variables in the dataset against each other, then concatenates them along a new dimension into a new array...
Read more >
How to reshape xarray dataset by collapsing coordinate
You can re-assign the "band" coordinate to a MultiIndex : In [4]: da = xr.DataArray(np.random.random((4, 4, 8)), dims=['x', 'y', ...
Read more >
numpy.reshape — NumPy v1.24 Manual
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found