|
| 1 | +.. _reshape: |
| 2 | + |
| 3 | +############################### |
| 4 | +Reshaping and reorganizing data |
| 5 | +############################### |
| 6 | + |
| 7 | +These methods allow you to reorganize |
| 8 | + |
| 9 | +.. ipython:: python |
| 10 | + :suppress: |
| 11 | +
|
| 12 | + import numpy as np |
| 13 | + import pandas as pd |
| 14 | + import xray |
| 15 | + np.random.seed(123456) |
| 16 | +
|
| 17 | +Reordering dimensions |
| 18 | +--------------------- |
| 19 | + |
| 20 | +To reorder dimensions on a :py:class:`~xray.DataArray` or across all variables |
| 21 | +on a :py:class:`~xray.Dataset`, use :py:meth:`xray.DataArray.transpose` or the |
| 22 | +``.T`` property: |
| 23 | + |
| 24 | +.. ipython:: python |
| 25 | +
|
| 26 | + ds = xray.Dataset({'foo': (('x', 'y', 'z'), [[[42]]]), 'bar': (('y', 'z'), [[24]])}) |
| 27 | + ds.transpose('y', 'z', 'x') |
| 28 | + ds.T |
| 29 | +
|
| 30 | +Converting between datasets and arrays |
| 31 | +-------------------------------------- |
| 32 | + |
| 33 | +To convert from a Dataset to a DataArray, use :py:meth:`~xray.Dataset.to_array`: |
| 34 | + |
| 35 | +.. ipython:: python |
| 36 | +
|
| 37 | + arr = ds.to_array() |
| 38 | + arr |
| 39 | +
|
| 40 | +This method broadcasts all data variables in the dataset against each other, |
| 41 | +then concatenates them along a new dimension into a new array while preserving |
| 42 | +coordinates. |
| 43 | + |
| 44 | +To convert back from a DataArray to a Dataset, use |
| 45 | +:py:meth:`~xray.DataArray.to_dataset`: |
| 46 | + |
| 47 | +.. ipython:: python |
| 48 | +
|
| 49 | + arr.to_dataset(dim='variable') |
| 50 | +
|
| 51 | +The broadcasting behavior of ``to_array`` means that the resulting array |
| 52 | +includes the union of data variable dimensions: |
| 53 | + |
| 54 | +.. ipython:: python |
| 55 | +
|
| 56 | + ds2 = xray.Dataset({'a': 0, 'b': ('x', [3, 4, 5])}) |
| 57 | +
|
| 58 | + # the input dataset has 4 elements |
| 59 | + ds2 |
| 60 | +
|
| 61 | + # the resulting array has 6 elements |
| 62 | + ds2.to_array() |
| 63 | +
|
| 64 | +Otherwise, the result could not be represented as an orthogonal array. |
| 65 | + |
| 66 | +If you use ``to_dataset`` without supplying the ``dim`` argument, the DataArray will be converted into a Dataset of one variable: |
| 67 | + |
| 68 | +.. ipython:: python |
| 69 | +
|
| 70 | + arr.to_dataset(name='combined') |
| 71 | +
|
| 72 | +.. _reshape.stack: |
| 73 | + |
| 74 | +Stack and unstack |
| 75 | +----------------- |
| 76 | + |
| 77 | +As part of xray's nascent support for :py:class:`pandas.MultiIndex`, we have |
| 78 | +implemented :py:meth:`~xray.DataArray.stack` and |
| 79 | +:py:meth:`~xray.DataArray.unstack` method, for combining or splitting dimensions: |
| 80 | + |
| 81 | +.. ipython:: python |
| 82 | +
|
| 83 | + array = xray.DataArray(np.random.randn(2, 3), |
| 84 | + coords=[('x', ['a', 'b']), ('y', [0, 1, 2])]) |
| 85 | + stacked = array.stack(z=('x', 'y')) |
| 86 | + stacked |
| 87 | + stacked.unstack('z') |
| 88 | +
|
| 89 | +These methods are modeled on the :py:class:`pandas.DataFrame` methods of the |
| 90 | +same name, although they in xray they always create new dimensions rather than |
| 91 | +adding to the existing index or columns. |
| 92 | + |
| 93 | +Like :py:meth:`DataFrame.unstack<pandas.DataFrame.unstack>`, xray's ``unstack`` always succeeds, even |
| 94 | +if the multi-index being unstacked does not contain all possible levels. Missing |
| 95 | +levels are filled in with ``NaN`` in the resulting object: |
| 96 | + |
| 97 | +.. ipython:: python |
| 98 | + |
| 99 | + stacked2 = stacked[::2] |
| 100 | + stacked2 |
| 101 | + stacked2.unstack('z') |
| 102 | +
|
| 103 | +However, xray's ``stack`` has an important difference from pandas: unlike |
| 104 | +pandas, it does not automatically drop missing values. Compare: |
| 105 | + |
| 106 | +.. ipython:: python |
| 107 | + |
| 108 | + array = xray.DataArray([[np.nan, 1], [2, 3]], dims=['x', 'y']) |
| 109 | + array.stack(z=('x', 'y')) |
| 110 | + array.to_pandas().stack() |
| 111 | +
|
| 112 | +We departed from pandas's behavior here because predictable shapes for new |
| 113 | +array dimensions is necessary for :ref:`dask`. |
| 114 | + |
| 115 | +Shift and roll |
| 116 | +-------------- |
| 117 | + |
| 118 | +To adjust coordinate labels, you can use the :py:meth:`~xray.Dataset.shift` and |
| 119 | +:py:meth:`~xray.Dataset.roll` methods: |
| 120 | + |
| 121 | +.. ipython:: python |
| 122 | +
|
| 123 | + array = xray.DataArray([1, 2, 3, 4], dims='x') |
| 124 | + array.shift(x=2) |
| 125 | + array.roll(x=2) |
0 commit comments