Skip to content

Commit 26f63e6

Browse files
committed
Doc cleanup
1 parent 7ea9de8 commit 26f63e6

File tree

7 files changed

+131
-84
lines changed

7 files changed

+131
-84
lines changed

doc/api.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Dataset contents
6565
Dataset.copy
6666
Dataset.merge
6767
Dataset.rename
68-
Dataset.drop_vars
68+
Dataset.drop
6969
Dataset.set_coords
7070
Dataset.reset_coords
7171

@@ -164,6 +164,7 @@ DataArray contents
164164
:toctree: generated/
165165

166166
DataArray.rename
167+
DataArray.drop
167168
DataArray.reset_coords
168169
DataArray.copy
169170

doc/combining.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ necessary.
7070

7171
.. _merge:
7272

73-
Merge and ``Dataset.__init__``
74-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
Merge
74+
~~~~~
7575

7676
To combine variables and coordinates between multiple Datasets, you can use the
7777
:py:meth:`~xray.Dataset.merge` and :py:meth:`~xray.Dataset.update` methods.
@@ -102,8 +102,8 @@ used in the :py:class:`~xray.Dataset` constructor:
102102
103103
.. _update:
104104

105-
Update and ``__setitem__``
106-
~~~~~~~~~~~~~~~~~~~~~~~~~~
105+
Update
106+
~~~~~~
107107

108108
In contrast, update modifies a dataset in-place without checking for conflicts,
109109
and will overwrite any existing variables with new values:
@@ -159,8 +159,8 @@ are constant along those new dimensions:
159159

160160
.. ipython:: python
161161
162-
left = Dataset(coords={'x': 0})
163-
right = Dataset({'x': [0, 0, 0]})
162+
left = xray.Dataset(coords={'x': 0})
163+
right = xray.Dataset({'x': [0, 0, 0]})
164164
left.broadcast_equals(right)
165165
166166
Like pandas objects, two xray objects are still equal or identical if they have

doc/examples/quick-overview.rst

+68-36
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Quick overview
33
##############
44

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.
88

99
To begin, import numpy, pandas and xray:
1010

@@ -22,29 +22,27 @@ array or list, with optional *dimensions* and *coordinates*:
2222

2323
.. ipython:: python
2424
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
2728
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:
2931

3032
.. ipython:: python
3133
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'))
3735
3836
Here are the key properties for a ``DataArray``:
3937

4038
.. ipython:: python
4139
4240
# 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
4644
# you can use this dictionary to store arbitrary metadata
47-
foo.attrs
45+
data.attrs
4846
4947
Indexing
5048
--------
@@ -55,16 +53,16 @@ pandas, because we borrow pandas' indexing machinery.
5553
.. ipython:: python
5654
5755
# positional and by integer label, like numpy
58-
foo[[0, 1]]
56+
data[[0, 1]]
5957
6058
# positional and by coordinate label, like pandas
61-
foo.loc['a':'b']
59+
data.loc['a':'b']
6260
6361
# by dimension name and integer label
64-
foo.isel(x=slice(2))
62+
data.isel(x=slice(2))
6563
6664
# by dimension name and coordinate label
67-
foo.sel(x=['a', 'b'])
65+
data.sel(x=['a', 'b'])
6866
6967
Computation
7068
-----------
@@ -73,30 +71,43 @@ Data arrays work very similarly to numpy ndarrays:
7371

7472
.. ipython:: python
7573
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()
8078
8179
However, aggregation operations can use dimension names instead of axis
8280
numbers:
8381

8482
.. ipython:: python
8583
86-
foo.mean(dim='x')
84+
data.mean(dim='x')
8785
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:
9088

9189
.. ipython:: python
9290
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')
9593
96-
bar
97-
zzz
94+
a
95+
b
9896
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]
100111
101112
GroupBy
102113
-------
@@ -105,10 +116,10 @@ xray supports grouped operations using a very similar API to pandas:
105116

106117
.. ipython:: python
107118
108-
labels = xray.DataArray(['E', 'F', 'E'], [foo.coords['y']], name='labels')
119+
labels = xray.DataArray(['E', 'F', 'E'], [data.coords['y']], name='labels')
109120
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())
112123
113124
Convert to pandas
114125
-----------------
@@ -117,5 +128,26 @@ A key feature of xray is robust conversion to and from pandas objects:
117128

118129
.. ipython:: python
119130
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')

doc/groupby.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ This last line is roughly equivalent to the following::
109109

110110
results = []
111111
for label, group in ds.groupby('letters'):
112-
results.append(group - alt.sel(label))
112+
results.append(group - alt.sel(x=label))
113113
xray.concat(results, dim='x')
114114

115115
Squeezing

doc/installing.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Optional dependencies:
1919
internal operations with xray data structures
2020

2121
Before you install xray, be sure you have the required dependencies (numpy and
22-
pandas) installed. The easiest way to do so is to use the
22+
pandas) installed. xray is a pure Python package, but its dependencies are not.
23+
The easiest way to get them installed is to use the
2324
`Anaconda python distribution <https://store.continuum.io/cshop/anaconda/>`__.
2425

2526
To install xray, use pip::

doc/io.rst

+16-30
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,19 @@ We can save a Dataset to disk using the
7373

7474
.. use verbatim because readthedocs doesn't have netCDF4 support
7575
76-
.. ipython::
77-
:verbatim:
76+
.. ipython:: python
7877
79-
In [1]: ds.to_netcdf('saved_on_disk.nc')
78+
ds.to_netcdf('saved_on_disk.nc')
8079
8180
By default, the file is saved as netCDF4.
8281

8382
We can load netCDF files to create a new Dataset using the
8483
:py:func:`~xray.open_dataset` function:
8584

86-
.. ipython::
87-
:verbatim:
88-
89-
In [1]: ds_disk = xray.open_dataset('saved_on_disk.nc')
85+
.. ipython:: python
9086
91-
In [2]: ds_disk
92-
Out[2]:
93-
<xray.Dataset>
94-
Dimensions: (x: 4, y: 5)
95-
Index Coordinates:
96-
x (x) int64 10 20 30 40
97-
y (y) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05
98-
Variables:
99-
foo (x, y) float64 0.127 0.9667 0.2605 0.8972 0.3767 0.3362 0.4514 0.8403 0.1231 ...
100-
z (x) |S1 'a' 'b' 'c' 'd'
87+
ds_disk = xray.open_dataset('saved_on_disk.nc')
88+
ds_disk
10189
10290
A dataset can also be loaded from a specific group within a netCDF
10391
file. To load from a group, pass a ``group`` keyword argument to the
@@ -114,24 +102,22 @@ section below.
114102

115103
xray's lazy loading of remote or on-disk datasets is often but not always
116104
desirable. Before performing computationally intense operations, it is
117-
usually a good idea to load a dataset entirely into memory by using the
118-
:py:meth:`~xray.Dataset.load_data` method:
119-
120-
.. ipython::
121-
:verbatim:
122-
123-
In [1]: ds_disk.load_data()
105+
usually a good idea to load a dataset entirely into memory by invoking the
106+
:py:meth:`~xray.Dataset.load_data` method.
124107

125108
Datasets have a :py:meth:`~xray.Dataset.close` method to close the associated
126109
netCDF file. However, it's often cleaner to use a ``with`` statement:
127110

128-
.. ipython::
129-
:verbatim:
111+
.. ipython:: python
112+
:suppress:
113+
114+
ds_disk.close()
115+
116+
.. ipython:: python
130117
131118
# this automatically closes the dataset after use
132-
In [100]: with xray.open_dataset('saved_on_disk.nc') as ds:
133-
... print(ds.keys())
134-
Out[100]: [u'foo', u'y', u'x', u'z']
119+
with xray.open_dataset('saved_on_disk.nc') as ds:
120+
print(ds.keys())
135121
136122
.. note::
137123

@@ -203,7 +189,7 @@ __ http://iri.columbia.edu/
203189
* X (X) float32 -125.0 -124.958 -124.917 -124.875 -124.833 -124.792 -124.75 ...
204190
* T (T) float32 -779.5 -778.5 -777.5 -776.5 -775.5 -774.5 -773.5 -772.5 -771.5 ...
205191
* Y (Y) float32 49.9167 49.875 49.8333 49.7917 49.75 49.7083 49.6667 49.625 ...
206-
Variables:
192+
Data variables:
207193
ppt (T, Y, X) float64 ...
208194
tdmean (T, Y, X) float64 ...
209195
tmax (T, Y, X) float64 ...

doc/whats-new.rst

+36-9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ Breaking changes
6262
These operations are lightning fast thanks to integration with bottleneck_,
6363
which is a new optional dependency for xray (numpy is used if bottleneck is
6464
not installed).
65+
- Scalar coordinates no longer conflict with constant arrays with the same
66+
value (e.g., in arithmetic, merging datasets and concat), even if they have
67+
different shape (:issue:`243`). For example, the coordinate ``c`` here
68+
persists through arithmetic, even though it has different shapes on each
69+
DataArray:
70+
71+
.. ipython:: python
72+
73+
a = xray.DataArray([1, 2], coords={'c': 0}, dims='x')
74+
b = xray.DataArray([1, 2], coords={'c': ('x', [0, 0])}, dims='x')
75+
(a + b).coords
76+
77+
This functionality can be controlled through the ``compat`` option, which
78+
has also been added to the :py:class:`~xray.Dataset` constructor.
6579
- We have updated our use of the terms of "coordinates" and "variables". What
6680
were known in previous versions of xray as "coordinates" and "variables" are
6781
now referred to throughout the documentation as "coordinate variables" and
@@ -80,24 +94,35 @@ Breaking changes
8094
ds['t.season']
8195
8296
Previously, it returned numbered seasons 1 through 4.
83-
- TODO: ``broadcast_equals`` method? ``drop`` method?
8497

8598
.. _bottleneck: https://github.com/kwgoodman/bottleneck
8699

87100
Enhancements
88101
~~~~~~~~~~~~
89102

90-
- Support for reindexing with a fill method. This will especially useful with
91-
pandas 0.16, which will support ``method='nearest'``.
103+
- Support for :py:meth:`~xray.Dataset.reindex` with a fill method. This
104+
provides a useful shortcut for upsampling:
105+
106+
.. ipython:: python
107+
108+
data = xray.DataArray([1, 2, 3], dims='x')
109+
data.reindex(x=[0.5, 1, 1.5, 2, 2.5], method='pad')
110+
111+
This will be especially useful once pandas 0.16 is released, at which point
112+
xray will immediately support reindexing with
113+
`method='nearest' <https://github.com/pydata/pandas/pull/9258>`_.
92114
- Use functions that return generic ndarrays with DataArray.groupby.apply and
93115
Dataset.apply (:issue:`327` and :issue:`329`). Thanks Jeff Gerard!
94-
- Consolidated the functionality of `dumps` (writing a dataset to a netCDF file
95-
as a bytestring) into the `to_netcdf` method (:issue:`333`).
96-
- `to_netcdf` now supports writing to groups in netCDF4 files (:issue:`333`).
97-
- `to_netcdf` now works when netcdf4-python is not installed as long as scipy
116+
- Consolidated the functionality of ``dumps`` (writing a dataset to a netCDF3
117+
bytestring) into :py:meth:`~xray.Dataset.to_netcdf` (:issue:`333`).
118+
- :py:meth:`~xray.Dataset.to_netcdf` now supports writing to groups in netCDF4
119+
files (:issue:`333`). It also now has a full docstring -- you should read it!
120+
- :py:func:`~xray.open_dataset` and :py:meth:`~xray.Dataset.to_netcdf` now
121+
work on netCDF4 files when netcdf4-python is not installed as long as scipy
98122
is available (:issue:`333`).
99-
- The new :py:meth:`~xray.Dataset.drop` method makes it easy to drop explicitly
100-
listed variables or index labels:
123+
- The new :py:meth:`xray.Dataset.drop <Dataset.drop>` and
124+
:py:meth:`xray.DataArray.drop <DataArray.drop>` methods makes it easy to drop
125+
explicitly listed variables or index labels:
101126

102127
.. ipython:: python
103128
@@ -109,6 +134,8 @@ Enhancements
109134
arr = xray.DataArray([1, 2, 3], coords=[('x', list('abc'))])
110135
arr.drop(['a', 'c'], dim='x')
111136
137+
- The :py:meth:`~xray.Dataset.broadcast_equals` has been added to correspond to
138+
the new ``compat`` option.
112139
- TODO: added a documentation example by Joe Hamman.
113140

114141
Bug fixes

0 commit comments

Comments
 (0)