Skip to content

Commit 76aa4f0

Browse files
authored
Ensure xarray imports (and docs build) even without pandas.Panel (#3058)
It's being removed in the next pandas 0.25 release (see pandas-dev/pandas#27101).
1 parent 2b3489b commit 76aa4f0

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

doc/pandas.rst

+27-13
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ However, you will need to set dimension names explicitly, either with the
151151
Transitioning from pandas.Panel to xarray
152152
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153153

154-
:py:class:`~pandas.Panel`, pandas' data structure for 3D arrays, has always
154+
``Panel``, pandas' data structure for 3D arrays, has always
155155
been a second class data structure compared to the Series and DataFrame. To
156156
allow pandas developers to focus more on its core functionality built around
157-
the DataFrame, pandas plans to eventually deprecate Panel.
157+
the DataFrame, panads has deprecated ``Panel``. It will be removed in pandas
158+
0.25.
158159

159160
xarray has most of ``Panel``'s features, a more explicit API (particularly around
160161
indexing), and the ability to scale to >3 dimensions with the same interface.
@@ -172,28 +173,41 @@ So you can represent a Panel, in two ways:
172173
Let's take a look:
173174

174175
.. ipython:: python
175-
:okwarning:
176176
177-
panel = pd.Panel(np.random.rand(2, 3, 4), items=list('ab'), major_axis=list('mno'),
178-
minor_axis=pd.date_range(start='2000', periods=4, name='date'))
179-
panel
177+
data = np.random.RandomState(0).rand(2, 3, 4)
178+
items = list('ab')
179+
major_axis = list('mno')
180+
minor_axis = pd.date_range(start='2000', periods=4, name='date')
180181
181-
As a DataArray:
182+
With old versions of pandas (prior to 0.25), this could stored in a ``Panel``:
183+
184+
.. ipython::
185+
:verbatim:
186+
187+
In [1]: pd.Panel(data, items, major_axis, minor_axis)
188+
Out[1]:
189+
<class 'pandas.core.panel.Panel'>
190+
Dimensions: 2 (items) x 3 (major_axis) x 4 (minor_axis)
191+
Items axis: a to b
192+
Major_axis axis: m to o
193+
Minor_axis axis: 2000-01-01 00:00:00 to 2000-01-04 00:00:00
194+
195+
To put this data in a ``DataArray``, write:
182196

183197
.. ipython:: python
184198
185-
# or equivalently, with Panel.to_xarray()
186-
xr.DataArray(panel)
199+
array = xr.DataArray(data, [items, major_axis, minor_axis])
200+
array
187201
188202
As you can see, there are three dimensions (each is also a coordinate). Two of
189-
the axes of the panel were unnamed, so have been assigned ``dim_0`` and
190-
``dim_1`` respectively, while the third retains its name ``date``.
203+
the axes of were unnamed, so have been assigned ``dim_0`` and ``dim_1``
204+
respectively, while the third retains its name ``date``.
191205

192-
As a Dataset:
206+
You can also easily convert this data into ``Dataset``:
193207

194208
.. ipython:: python
195209
196-
xr.Dataset(panel)
210+
array.to_dataset(dim='dim_0')
197211
198212
Here, there are two data variables, each representing a DataFrame on panel's
199213
``items`` axis, and labelled as such. Each variable is a 2D array of the

doc/whats-new.rst

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Enhancements
2424
Bug fixes
2525
~~~~~~~~~
2626

27+
- Fix to ensure that xarray can still be imported with pandas 0.25, which will
28+
remove `pd.Panel`.
29+
By `Stephan Hoyer <https://github.com/shoyer>`_.
30+
2731
.. _whats-new.0.12.2:
2832

2933
v0.12.2 (29 June 2019)
@@ -164,6 +168,12 @@ Bug fixes
164168
By `Maximilian Roos <https://github.com/max-sixty>`_.
165169
- Fixed performance issues with cftime installed (:issue:`3000`)
166170
By `0x0L <https://github.com/0x0L>`_.
171+
- Replace incorrect usages of `message` in pytest assertions
172+
with `match` (:issue:`3011`)
173+
By `Maximilian Roos <https://github.com/max-sixty>`_.
174+
- Add explicit pytest markers, now required by pytest
175+
(:issue:`3032`).
176+
By `Maximilian Roos <https://github.com/max-sixty>`_.
167177
- Test suite fixes for newer versions of pytest (:issue:`3011`, :issue:`3032`).
168178
By `Maximilian Roos <https://github.com/max-sixty>`_
169179
and `Stephan Hoyer <https://github.com/shoyer>`_.

xarray/core/dataarray.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def __init__(
264264
coords = [data.index, data.columns]
265265
elif isinstance(data, (pd.Index, IndexVariable)):
266266
coords = [data]
267-
elif isinstance(data, pd.Panel):
267+
elif hasattr(pd, 'Panel') and isinstance(data, pd.Panel):
268268
coords = [data.items, data.major_axis, data.minor_axis]
269269
if dims is None:
270270
dims = getattr(data, 'dims', getattr(coords, 'dims', None))
@@ -1825,8 +1825,7 @@ def reduce(self, func: Callable[..., Any],
18251825
**kwargs)
18261826
return self._replace_maybe_drop_dims(var)
18271827

1828-
def to_pandas(self) -> Union[
1829-
'DataArray', pd.Series, pd.DataFrame, pd.Panel]:
1828+
def to_pandas(self) -> Union['DataArray', pd.Series, pd.DataFrame]:
18301829
"""Convert this array into a pandas object with the same shape.
18311830
18321831
The type of the returned object depends on the number of DataArray
@@ -1845,8 +1844,9 @@ def to_pandas(self) -> Union[
18451844
# attributes that correspond to their indexes into a separate module?
18461845
constructors = {0: lambda x: x,
18471846
1: pd.Series,
1848-
2: pd.DataFrame,
1849-
3: pd.Panel}
1847+
2: pd.DataFrame}
1848+
if hasattr(pd, 'Panel'):
1849+
constructors[3] = pd.Panel
18501850
try:
18511851
constructor = constructors[self.ndim]
18521852
except KeyError:

xarray/core/merge.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from .dataset import Dataset
1616

1717

18-
PANDAS_TYPES = (pd.Series, pd.DataFrame, pd.Panel)
18+
PANDAS_TYPES = (
19+
(pd.Series, pd.DataFrame) + (pd.Panel,) if hasattr(pd, 'Panel') else ()
20+
)
1921

2022
_VALID_COMPAT = Frozen({'identical': 0,
2123
'equals': 1,

0 commit comments

Comments
 (0)