@@ -4,11 +4,12 @@ Combining data
4
4
--------------
5
5
6
6
.. ipython :: python
7
- :suppress:
7
+ :suppress:
8
8
9
9
import numpy as np
10
10
import pandas as pd
11
11
import xarray as xr
12
+
12
13
np.random.seed(123456 )
13
14
14
15
* For combining datasets or data arrays along a single dimension, see concatenate _.
@@ -28,11 +29,10 @@ that dimension:
28
29
29
30
.. ipython :: python
30
31
31
- arr = xr.DataArray(np.random.randn(2 , 3 ),
32
- [(' x' , [' a' , ' b' ]), (' y' , [10 , 20 , 30 ])])
32
+ arr = xr.DataArray(np.random.randn(2 , 3 ), [(" x" , [" a" , " b" ]), (" y" , [10 , 20 , 30 ])])
33
33
arr[:, :1 ]
34
34
# this resembles how you would use np.concatenate
35
- xr.concat([arr[:, :1 ], arr[:, 1 :]], dim = ' y ' )
35
+ xr.concat([arr[:, :1 ], arr[:, 1 :]], dim = " y " )
36
36
37
37
In addition to combining along an existing dimension, ``concat `` can create a
38
38
new dimension by stacking lower dimensional arrays together:
@@ -41,30 +41,30 @@ new dimension by stacking lower dimensional arrays together:
41
41
42
42
arr[0 ]
43
43
# to combine these 1d arrays into a 2d array in numpy, you would use np.array
44
- xr.concat([arr[0 ], arr[1 ]], ' x ' )
44
+ xr.concat([arr[0 ], arr[1 ]], " x " )
45
45
46
46
If the second argument to ``concat `` is a new dimension name, the arrays will
47
47
be concatenated along that new dimension, which is always inserted as the first
48
48
dimension:
49
49
50
50
.. ipython :: python
51
51
52
- xr.concat([arr[0 ], arr[1 ]], ' new_dim' )
52
+ xr.concat([arr[0 ], arr[1 ]], " new_dim" )
53
53
54
54
The second argument to ``concat `` can also be an :py:class: `~pandas.Index ` or
55
55
:py:class: `~xarray.DataArray ` object as well as a string, in which case it is
56
56
used to label the values along the new dimension:
57
57
58
58
.. ipython :: python
59
59
60
- xr.concat([arr[0 ], arr[1 ]], pd.Index([- 90 , - 100 ], name = ' new_dim' ))
60
+ xr.concat([arr[0 ], arr[1 ]], pd.Index([- 90 , - 100 ], name = " new_dim" ))
61
61
62
62
Of course, ``concat `` also works on ``Dataset `` objects:
63
63
64
64
.. ipython :: python
65
65
66
- ds = arr.to_dataset(name = ' foo' )
67
- xr.concat([ds.sel(x = ' a ' ), ds.sel(x = ' b ' )], ' x ' )
66
+ ds = arr.to_dataset(name = " foo" )
67
+ xr.concat([ds.sel(x = " a " ), ds.sel(x = " b " )], " x " )
68
68
69
69
:py:func: `~xarray.concat ` has a number of options which provide deeper control
70
70
over which variables are concatenated and how it handles conflicting variables
@@ -84,16 +84,16 @@ To combine variables and coordinates between multiple ``DataArray`` and/or
84
84
85
85
.. ipython :: python
86
86
87
- xr.merge([ds, ds.rename({' foo' : ' bar' })])
88
- xr.merge([xr.DataArray(n, name = ' var%d ' % n) for n in range (5 )])
87
+ xr.merge([ds, ds.rename({" foo" : " bar" })])
88
+ xr.merge([xr.DataArray(n, name = " var%d " % n) for n in range (5 )])
89
89
90
90
If you merge another dataset (or a dictionary including data array objects), by
91
91
default the resulting dataset will be aligned on the **union ** of all index
92
92
coordinates:
93
93
94
94
.. ipython :: python
95
95
96
- other = xr.Dataset({' bar' : (' x ' , [1 , 2 , 3 , 4 ]), ' x ' : list (' abcd' )})
96
+ other = xr.Dataset({" bar" : (" x " , [1 , 2 , 3 , 4 ]), " x " : list (" abcd" )})
97
97
xr.merge([ds, other])
98
98
99
99
This ensures that ``merge `` is non-destructive. ``xarray.MergeError `` is raised
@@ -116,7 +116,7 @@ used in the :py:class:`~xarray.Dataset` constructor:
116
116
117
117
.. ipython :: python
118
118
119
- xr.Dataset({' a ' : arr[:- 1 ], ' b ' : arr[1 :]})
119
+ xr.Dataset({" a " : arr[:- 1 ], " b " : arr[1 :]})
120
120
121
121
.. _combine :
122
122
@@ -131,8 +131,8 @@ are filled with ``NaN``. For example:
131
131
132
132
.. ipython :: python
133
133
134
- ar0 = xr.DataArray([[0 , 0 ], [0 , 0 ]], [(' x ' , [' a ' , ' b ' ]), (' y ' , [- 1 , 0 ])])
135
- ar1 = xr.DataArray([[1 , 1 ], [1 , 1 ]], [(' x ' , [' b ' , ' c ' ]), (' y ' , [0 , 1 ])])
134
+ ar0 = xr.DataArray([[0 , 0 ], [0 , 0 ]], [(" x " , [" a " , " b " ]), (" y " , [- 1 , 0 ])])
135
+ ar1 = xr.DataArray([[1 , 1 ], [1 , 1 ]], [(" x " , [" b " , " c " ]), (" y " , [0 , 1 ])])
136
136
ar0.combine_first(ar1)
137
137
ar1.combine_first(ar0)
138
138
@@ -152,7 +152,7 @@ variables with new values:
152
152
153
153
.. ipython :: python
154
154
155
- ds.update({' space' : (' space' , [10.2 , 9.4 , 3.9 ])})
155
+ ds.update({" space" : (" space" , [10.2 , 9.4 , 3.9 ])})
156
156
157
157
However, dimensions are still required to be consistent between different
158
158
Dataset variables, so you cannot change the size of a dimension unless you
@@ -170,7 +170,7 @@ syntax:
170
170
171
171
.. ipython :: python
172
172
173
- ds[' baz' ] = xr.DataArray([9 , 9 , 9 , 9 , 9 ], coords = [(' x ' , list (' abcde' ))])
173
+ ds[" baz" ] = xr.DataArray([9 , 9 , 9 , 9 , 9 ], coords = [(" x " , list (" abcde" ))])
174
174
ds.baz
175
175
176
176
Equals and identical
@@ -193,16 +193,16 @@ object:
193
193
194
194
.. ipython :: python
195
195
196
- arr.identical(arr.rename(' bar' ))
196
+ arr.identical(arr.rename(" bar" ))
197
197
198
198
:py:attr: `~xarray.Dataset.broadcast_equals ` does a more relaxed form of equality
199
199
check that allows variables to have different dimensions, as long as values
200
200
are constant along those new dimensions:
201
201
202
202
.. ipython :: python
203
203
204
- left = xr.Dataset(coords = {' x ' : 0 })
205
- right = xr.Dataset({' x ' : [0 , 0 , 0 ]})
204
+ left = xr.Dataset(coords = {" x " : 0 })
205
+ right = xr.Dataset({" x " : [0 , 0 , 0 ]})
206
206
left.broadcast_equals(right)
207
207
208
208
Like pandas objects, two xarray objects are still equal or identical if they have
@@ -231,9 +231,9 @@ coordinates as long as any non-missing values agree or are disjoint:
231
231
232
232
.. ipython :: python
233
233
234
- ds1 = xr.Dataset({' a ' : (' x ' , [10 , 20 , 30 , np.nan])}, {' x ' : [1 , 2 , 3 , 4 ]})
235
- ds2 = xr.Dataset({' a ' : (' x ' , [np.nan, 30 , 40 , 50 ])}, {' x ' : [2 , 3 , 4 , 5 ]})
236
- xr.merge([ds1, ds2], compat = ' no_conflicts' )
234
+ ds1 = xr.Dataset({" a " : (" x " , [10 , 20 , 30 , np.nan])}, {" x " : [1 , 2 , 3 , 4 ]})
235
+ ds2 = xr.Dataset({" a " : (" x " , [np.nan, 30 , 40 , 50 ])}, {" x " : [2 , 3 , 4 , 5 ]})
236
+ xr.merge([ds1, ds2], compat = " no_conflicts" )
237
237
238
238
Note that due to the underlying representation of missing values as floating
239
239
point numbers (``NaN ``), variable data type is not always preserved when merging
@@ -273,10 +273,12 @@ datasets into a doubly-nested list, e.g:
273
273
274
274
.. ipython :: python
275
275
276
- arr = xr.DataArray(name = ' temperature' , data = np.random.randint(5 , size = (2 , 2 )), dims = [' x' , ' y' ])
276
+ arr = xr.DataArray(
277
+ name = " temperature" , data = np.random.randint(5 , size = (2 , 2 )), dims = [" x" , " y" ]
278
+ )
277
279
arr
278
280
ds_grid = [[arr, arr], [arr, arr]]
279
- xr.combine_nested(ds_grid, concat_dim = [' x ' , ' y ' ])
281
+ xr.combine_nested(ds_grid, concat_dim = [" x " , " y " ])
280
282
281
283
:py:func: `~xarray.combine_nested ` can also be used to explicitly merge datasets
282
284
with different variables. For example if we have 4 datasets, which are divided
@@ -286,10 +288,10 @@ we wish to use ``merge`` instead of ``concat``:
286
288
287
289
.. ipython :: python
288
290
289
- temp = xr.DataArray(name = ' temperature' , data = np.random.randn(2 ), dims = [' t ' ])
290
- precip = xr.DataArray(name = ' precipitation' , data = np.random.randn(2 ), dims = [' t ' ])
291
+ temp = xr.DataArray(name = " temperature" , data = np.random.randn(2 ), dims = [" t " ])
292
+ precip = xr.DataArray(name = " precipitation" , data = np.random.randn(2 ), dims = [" t " ])
291
293
ds_grid = [[temp, precip], [temp, precip]]
292
- xr.combine_nested(ds_grid, concat_dim = [' t ' , None ])
294
+ xr.combine_nested(ds_grid, concat_dim = [" t " , None ])
293
295
294
296
:py:func: `~xarray.combine_by_coords ` is for combining objects which have dimension
295
297
coordinates which specify their relationship to and order relative to one
@@ -302,8 +304,8 @@ coordinates, not on their position in the list passed to ``combine_by_coords``.
302
304
.. ipython :: python
303
305
:okwarning:
304
306
305
- x1 = xr.DataArray(name = ' foo' , data = np.random.randn(3 ), coords = [(' x ' , [0 , 1 , 2 ])])
306
- x2 = xr.DataArray(name = ' foo' , data = np.random.randn(3 ), coords = [(' x ' , [3 , 4 , 5 ])])
307
+ x1 = xr.DataArray(name = " foo" , data = np.random.randn(3 ), coords = [(" x " , [0 , 1 , 2 ])])
308
+ x2 = xr.DataArray(name = " foo" , data = np.random.randn(3 ), coords = [(" x " , [3 , 4 , 5 ])])
307
309
xr.combine_by_coords([x2, x1])
308
310
309
311
These functions can be used by :py:func: `~xarray.open_mfdataset ` to open many
0 commit comments