2
2
Quick overview
3
3
##############
4
4
5
- Here are some quick examples of what you can do with xray's
6
- :py:class: ` ~xray.DataArray ` object. Everything is explained in much more
7
- detail in the rest of the documentation.
5
+ Here are some quick examples of what you can do with :py:class: ` xray.DataArray `
6
+ objects. Everything is explained in much more detail in the rest of the
7
+ documentation.
8
8
9
9
To begin, import numpy, pandas and xray:
10
10
@@ -22,29 +22,27 @@ array or list, with optional *dimensions* and *coordinates*:
22
22
23
23
.. ipython :: python
24
24
25
- xray.DataArray(np.random.randn(2 , 3 ))
26
- xray.DataArray(np.random.randn(2 , 3 ), [(' x' , [' a' , ' b' ]), (' y' , [- 2 , 0 , 2 ])])
25
+ xray.DataArray(np.random.randn(2 , 3 ))
26
+ data = xray.DataArray(np.random.randn(2 , 3 ), [(' x' , [' a' , ' b' ]), (' y' , [- 2 , 0 , 2 ])])
27
+ data
27
28
28
- You can also pass in pandas data structures directly:
29
+ If you supply a pandas :py:class: `~pandas.Series ` or
30
+ :py:class: `~pandas.DataFrame `, metadata is copied directly:
29
31
30
32
.. ipython :: python
31
33
32
- df = pd.DataFrame(np.random.randn(2 , 3 ), index = [' a' , ' b' ], columns = [- 2 , 0 , 2 ])
33
- df.index.name = ' x'
34
- df.columns.name = ' y'
35
- foo = xray.DataArray(df, name = ' foo' )
36
- foo
34
+ xray.DataArray(pd.Series(range (3 ), index = list (' abc' ), name = ' foo' ))
37
35
38
36
Here are the key properties for a ``DataArray ``:
39
37
40
38
.. ipython :: python
41
39
42
40
# like in pandas, values is a numpy array that you can modify in-place
43
- foo .values
44
- foo .dims
45
- foo .coords[ ' y ' ]
41
+ data .values
42
+ data .dims
43
+ data .coords
46
44
# you can use this dictionary to store arbitrary metadata
47
- foo .attrs
45
+ data .attrs
48
46
49
47
Indexing
50
48
--------
@@ -55,16 +53,16 @@ pandas, because we borrow pandas' indexing machinery.
55
53
.. ipython :: python
56
54
57
55
# positional and by integer label, like numpy
58
- foo [[0 , 1 ]]
56
+ data [[0 , 1 ]]
59
57
60
58
# positional and by coordinate label, like pandas
61
- foo .loc[' a' :' b' ]
59
+ data .loc[' a' :' b' ]
62
60
63
61
# by dimension name and integer label
64
- foo .isel(x = slice (2 ))
62
+ data .isel(x = slice (2 ))
65
63
66
64
# by dimension name and coordinate label
67
- foo .sel(x = [' a' , ' b' ])
65
+ data .sel(x = [' a' , ' b' ])
68
66
69
67
Computation
70
68
-----------
@@ -73,30 +71,43 @@ Data arrays work very similarly to numpy ndarrays:
73
71
74
72
.. ipython :: python
75
73
76
- foo + 10
77
- np.sin(foo )
78
- foo .T
79
- foo .sum()
74
+ data + 10
75
+ np.sin(data )
76
+ data .T
77
+ data .sum()
80
78
81
79
However, aggregation operations can use dimension names instead of axis
82
80
numbers:
83
81
84
82
.. ipython :: python
85
83
86
- foo .mean(dim = ' x' )
84
+ data .mean(dim = ' x' )
87
85
88
- Arithmetic operations broadcast based on dimension name, so you don't need to
89
- insert dummy dimensions for alignment:
86
+ Arithmetic operations broadcast based on dimension name. This means you don't
87
+ need to insert dummy dimensions for alignment:
90
88
91
89
.. ipython :: python
92
90
93
- bar = xray.DataArray(np.random.randn(3 ), [foo .coords[' y' ]])
94
- zzz = xray.DataArray(np.random.randn(4 ), dims = ' z' )
91
+ a = xray.DataArray(np.random.randn(3 ), [data .coords[' y' ]])
92
+ b = xray.DataArray(np.random.randn(4 ), dims = ' z' )
95
93
96
- bar
97
- zzz
94
+ a
95
+ b
98
96
99
- bar + zzz
97
+ a + b
98
+
99
+ It also means that in most cases you do not need to worry about the order of
100
+ dimensions:
101
+
102
+ .. ipython :: python
103
+
104
+ data - data.T
105
+
106
+ Operations also align based on index labels:
107
+
108
+ .. ipython :: python
109
+
110
+ data[:- 1 ] - data[:1 ]
100
111
101
112
GroupBy
102
113
-------
@@ -105,10 +116,10 @@ xray supports grouped operations using a very similar API to pandas:
105
116
106
117
.. ipython :: python
107
118
108
- labels = xray.DataArray([' E' , ' F' , ' E' ], [foo .coords[' y' ]], name = ' labels' )
119
+ labels = xray.DataArray([' E' , ' F' , ' E' ], [data .coords[' y' ]], name = ' labels' )
109
120
labels
110
- foo .groupby(labels).mean(' y' )
111
- foo .groupby(labels).apply(lambda x : x - x.min())
121
+ data .groupby(labels).mean(' y' )
122
+ data .groupby(labels).apply(lambda x : x - x.min())
112
123
113
124
Convert to pandas
114
125
-----------------
@@ -117,5 +128,26 @@ A key feature of xray is robust conversion to and from pandas objects:
117
128
118
129
.. ipython :: python
119
130
120
- foo.to_series()
121
- foo.to_pandas()
131
+ data.to_series()
132
+ data.to_pandas()
133
+
134
+ Datasets and NetCDF
135
+ -------------------
136
+
137
+ :py:class: `xray.Dataset ` is a dict-like container of ``DataArray `` objects that share
138
+ index labels and dimensions. It looks a lot like a netCDF file:
139
+
140
+ .. ipython :: python
141
+
142
+ ds = data.to_dataset()
143
+ ds
144
+
145
+ You can do almost everything you can do with ``DataArray `` objects with
146
+ ``Dataset `` objects if you prefer to work with multiple variables at once.
147
+
148
+ Datasets also let you easily read and write netCDF files:
149
+
150
+ .. ipython :: python
151
+
152
+ ds.to_netcdf(' example.nc' )
153
+ xray.open_dataset(' example.nc' )
0 commit comments