Skip to content
forked from pydata/xarray

Commit 8f464c4

Browse files
committed
Merge remote-tracking branch 'upstream/master' into indexvariable-values
* upstream/master: add spacing in the versions section of the issue report (pydata#3876) map_blocks: allow user function to add new unindexed dimension. (pydata#3817) Delete associated indexes when deleting coordinate variables. (pydata#3840) remove macos build while waiting for libwebp fix (pydata#3875) Fix html repr on non-str keys (pydata#3870) Allow ellipsis to be used in stack (pydata#3826) Improve where docstring (pydata#3836) Add DataArray.pad, Dataset.pad, Variable.pad (pydata#3596) Fix some warnings (pydata#3864) Feature/weighted (pydata#2922) Fix recombination in groupby when changing size along the grouped dimension (pydata#3807) Blacken the doctest code in docstrings (pydata#3857) Fix multi-index with categorical values. (pydata#3860) Fix interp bug when indexer shares coordinates with array (pydata#3758) Fix alignment with join="override" when some dims are unindexed (pydata#3839)
2 parents 705c7eb + 6c19aab commit 8f464c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2184
-422
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ assignees: ''
2828
#### Versions
2929

3030
<details><summary>Output of `xr.show_versions()`</summary>
31+
3132
<!-- Paste the output here xr.show_versions() here -->
3233

34+
3335
</details>

azure-pipelines.yml

+10-9
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ jobs:
3232
steps:
3333
- template: ci/azure/unit-tests.yml
3434

35-
- job: MacOSX
36-
strategy:
37-
matrix:
38-
py38:
39-
conda_env: py38
40-
pool:
41-
vmImage: 'macOS-10.15'
42-
steps:
43-
- template: ci/azure/unit-tests.yml
35+
# excluded while waiting for https://github.com/conda-forge/libwebp-feedstock/issues/26
36+
# - job: MacOSX
37+
# strategy:
38+
# matrix:
39+
# py38:
40+
# conda_env: py38
41+
# pool:
42+
# vmImage: 'macOS-10.15'
43+
# steps:
44+
# - template: ci/azure/unit-tests.yml
4445

4546
- job: Windows
4647
strategy:

doc/api-hidden.rst

-2
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@
379379
Variable.min
380380
Variable.no_conflicts
381381
Variable.notnull
382-
Variable.pad_with_fill_value
383382
Variable.prod
384383
Variable.quantile
385384
Variable.rank
@@ -453,7 +452,6 @@
453452
IndexVariable.min
454453
IndexVariable.no_conflicts
455454
IndexVariable.notnull
456-
IndexVariable.pad_with_fill_value
457455
IndexVariable.prod
458456
IndexVariable.quantile
459457
IndexVariable.rank

doc/api.rst

+20
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Computation
165165
Dataset.groupby_bins
166166
Dataset.rolling
167167
Dataset.rolling_exp
168+
Dataset.weighted
168169
Dataset.coarsen
169170
Dataset.resample
170171
Dataset.diff
@@ -220,6 +221,7 @@ Reshaping and reorganizing
220221
Dataset.to_stacked_array
221222
Dataset.shift
222223
Dataset.roll
224+
Dataset.pad
223225
Dataset.sortby
224226
Dataset.broadcast_like
225227

@@ -340,6 +342,7 @@ Computation
340342
DataArray.groupby_bins
341343
DataArray.rolling
342344
DataArray.rolling_exp
345+
DataArray.weighted
343346
DataArray.coarsen
344347
DataArray.dt
345348
DataArray.resample
@@ -399,6 +402,7 @@ Reshaping and reorganizing
399402
DataArray.to_unstacked_dataset
400403
DataArray.shift
401404
DataArray.roll
405+
DataArray.pad
402406
DataArray.sortby
403407
DataArray.broadcast_like
404408

@@ -577,6 +581,22 @@ Rolling objects
577581
core.rolling.DatasetRolling.reduce
578582
core.rolling_exp.RollingExp
579583

584+
Weighted objects
585+
================
586+
587+
.. autosummary::
588+
:toctree: generated/
589+
590+
core.weighted.DataArrayWeighted
591+
core.weighted.DataArrayWeighted.mean
592+
core.weighted.DataArrayWeighted.sum
593+
core.weighted.DataArrayWeighted.sum_of_weights
594+
core.weighted.DatasetWeighted
595+
core.weighted.DatasetWeighted.mean
596+
core.weighted.DatasetWeighted.sum
597+
core.weighted.DatasetWeighted.sum_of_weights
598+
599+
580600
Coarsen objects
581601
===============
582602

doc/computation.rst

+85-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. currentmodule:: xarray
2+
13
.. _comput:
24

35
###########
@@ -241,12 +243,94 @@ You can also use ``construct`` to compute a weighted rolling sum:
241243
To avoid this, use ``skipna=False`` as the above example.
242244

243245

246+
.. _comput.weighted:
247+
248+
Weighted array reductions
249+
=========================
250+
251+
:py:class:`DataArray` and :py:class:`Dataset` objects include :py:meth:`DataArray.weighted`
252+
and :py:meth:`Dataset.weighted` array reduction methods. They currently
253+
support weighted ``sum`` and weighted ``mean``.
254+
255+
.. ipython:: python
256+
257+
coords = dict(month=('month', [1, 2, 3]))
258+
259+
prec = xr.DataArray([1.1, 1.0, 0.9], dims=('month', ), coords=coords)
260+
weights = xr.DataArray([31, 28, 31], dims=('month', ), coords=coords)
261+
262+
Create a weighted object:
263+
264+
.. ipython:: python
265+
266+
weighted_prec = prec.weighted(weights)
267+
weighted_prec
268+
269+
Calculate the weighted sum:
270+
271+
.. ipython:: python
272+
273+
weighted_prec.sum()
274+
275+
Calculate the weighted mean:
276+
277+
.. ipython:: python
278+
279+
weighted_prec.mean(dim="month")
280+
281+
The weighted sum corresponds to:
282+
283+
.. ipython:: python
284+
285+
weighted_sum = (prec * weights).sum()
286+
weighted_sum
287+
288+
and the weighted mean to:
289+
290+
.. ipython:: python
291+
292+
weighted_mean = weighted_sum / weights.sum()
293+
weighted_mean
294+
295+
However, the functions also take missing values in the data into account:
296+
297+
.. ipython:: python
298+
299+
data = xr.DataArray([np.NaN, 2, 4])
300+
weights = xr.DataArray([8, 1, 1])
301+
302+
data.weighted(weights).mean()
303+
304+
Using ``(data * weights).sum() / weights.sum()`` would (incorrectly) result
305+
in 0.6.
306+
307+
308+
If the weights add up to to 0, ``sum`` returns 0:
309+
310+
.. ipython:: python
311+
312+
data = xr.DataArray([1.0, 1.0])
313+
weights = xr.DataArray([-1.0, 1.0])
314+
315+
data.weighted(weights).sum()
316+
317+
and ``mean`` returns ``NaN``:
318+
319+
.. ipython:: python
320+
321+
data.weighted(weights).mean()
322+
323+
324+
.. note::
325+
``weights`` must be a :py:class:`DataArray` and cannot contain missing values.
326+
Missing values can be replaced manually by ``weights.fillna(0)``.
327+
244328
.. _comput.coarsen:
245329

246330
Coarsen large arrays
247331
====================
248332

249-
``DataArray`` and ``Dataset`` objects include a
333+
:py:class:`DataArray` and :py:class:`Dataset` objects include a
250334
:py:meth:`~xarray.DataArray.coarsen` and :py:meth:`~xarray.Dataset.coarsen`
251335
methods. This supports the block aggregation along multiple dimensions,
252336

doc/examples.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Examples
66

77
examples/weather-data
88
examples/monthly-means
9+
examples/area_weighted_temperature
910
examples/multidimensional-coords
1011
examples/visualization_gallery
1112
examples/ROMS_ocean_model

0 commit comments

Comments
 (0)