@@ -151,10 +151,11 @@ However, you will need to set dimension names explicitly, either with the
151
151
Transitioning from pandas.Panel to xarray
152
152
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153
153
154
- :py:class: ` ~pandas. Panel `, pandas' data structure for 3D arrays, has always
154
+ `` Panel ` `, pandas' data structure for 3D arrays, has always
155
155
been a second class data structure compared to the Series and DataFrame. To
156
156
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.
158
159
159
160
xarray has most of ``Panel ``'s features, a more explicit API (particularly around
160
161
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:
172
173
Let's take a look:
173
174
174
175
.. ipython :: python
175
- :okwarning:
176
176
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' )
180
181
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:
182
196
183
197
.. ipython :: python
184
198
185
- # or equivalently, with Panel.to_xarray( )
186
- xr.DataArray(panel)
199
+ array = xr.DataArray(data, [items, major_axis, minor_axis] )
200
+ array
187
201
188
202
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 ``.
191
205
192
- As a Dataset:
206
+ You can also easily convert this data into `` Dataset `` :
193
207
194
208
.. ipython :: python
195
209
196
- xr.Dataset(panel )
210
+ array.to_dataset( dim = ' dim_0 ' )
197
211
198
212
Here, there are two data variables, each representing a DataFrame on panel's
199
213
``items `` axis, and labelled as such. Each variable is a 2D array of the
0 commit comments