Document ways to reshape a DataArray
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
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()
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided xr_reshape example, especially pandas.MultiIndex.from_product, DataArray.unstack(), and DataArray.transpose(). Determine where a DataArray method would fit and verify that the example produces dimensions ('year', 'month', 'lat', 'lon') when the reshape is complete.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100