Skip to content
forked from pydata/xarray

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into indexvariable-values
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
dcherian committed Mar 22, 2020
2 parents 705c7eb + 6c19aab commit 8f464c4
Show file tree
Hide file tree
Showing 52 changed files with 2,184 additions and 422 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ assignees: ''
#### Versions

<details><summary>Output of `xr.show_versions()`</summary>

<!-- Paste the output here xr.show_versions() here -->


</details>
19 changes: 10 additions & 9 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ jobs:
steps:
- template: ci/azure/unit-tests.yml

- job: MacOSX
strategy:
matrix:
py38:
conda_env: py38
pool:
vmImage: 'macOS-10.15'
steps:
- template: ci/azure/unit-tests.yml
# excluded while waiting for https://github.com/conda-forge/libwebp-feedstock/issues/26
# - job: MacOSX
# strategy:
# matrix:
# py38:
# conda_env: py38
# pool:
# vmImage: 'macOS-10.15'
# steps:
# - template: ci/azure/unit-tests.yml

- job: Windows
strategy:
Expand Down
2 changes: 0 additions & 2 deletions doc/api-hidden.rst
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@
Variable.min
Variable.no_conflicts
Variable.notnull
Variable.pad_with_fill_value
Variable.prod
Variable.quantile
Variable.rank
Expand Down Expand Up @@ -453,7 +452,6 @@
IndexVariable.min
IndexVariable.no_conflicts
IndexVariable.notnull
IndexVariable.pad_with_fill_value
IndexVariable.prod
IndexVariable.quantile
IndexVariable.rank
Expand Down
20 changes: 20 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Computation
Dataset.groupby_bins
Dataset.rolling
Dataset.rolling_exp
Dataset.weighted
Dataset.coarsen
Dataset.resample
Dataset.diff
Expand Down Expand Up @@ -220,6 +221,7 @@ Reshaping and reorganizing
Dataset.to_stacked_array
Dataset.shift
Dataset.roll
Dataset.pad
Dataset.sortby
Dataset.broadcast_like

Expand Down Expand Up @@ -340,6 +342,7 @@ Computation
DataArray.groupby_bins
DataArray.rolling
DataArray.rolling_exp
DataArray.weighted
DataArray.coarsen
DataArray.dt
DataArray.resample
Expand Down Expand Up @@ -399,6 +402,7 @@ Reshaping and reorganizing
DataArray.to_unstacked_dataset
DataArray.shift
DataArray.roll
DataArray.pad
DataArray.sortby
DataArray.broadcast_like

Expand Down Expand Up @@ -577,6 +581,22 @@ Rolling objects
core.rolling.DatasetRolling.reduce
core.rolling_exp.RollingExp

Weighted objects
================

.. autosummary::
:toctree: generated/

core.weighted.DataArrayWeighted
core.weighted.DataArrayWeighted.mean
core.weighted.DataArrayWeighted.sum
core.weighted.DataArrayWeighted.sum_of_weights
core.weighted.DatasetWeighted
core.weighted.DatasetWeighted.mean
core.weighted.DatasetWeighted.sum
core.weighted.DatasetWeighted.sum_of_weights


Coarsen objects
===============

Expand Down
86 changes: 85 additions & 1 deletion doc/computation.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. currentmodule:: xarray

.. _comput:

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


.. _comput.weighted:

Weighted array reductions
=========================

:py:class:`DataArray` and :py:class:`Dataset` objects include :py:meth:`DataArray.weighted`
and :py:meth:`Dataset.weighted` array reduction methods. They currently
support weighted ``sum`` and weighted ``mean``.

.. ipython:: python

coords = dict(month=('month', [1, 2, 3]))

prec = xr.DataArray([1.1, 1.0, 0.9], dims=('month', ), coords=coords)
weights = xr.DataArray([31, 28, 31], dims=('month', ), coords=coords)

Create a weighted object:

.. ipython:: python

weighted_prec = prec.weighted(weights)
weighted_prec

Calculate the weighted sum:

.. ipython:: python

weighted_prec.sum()

Calculate the weighted mean:

.. ipython:: python

weighted_prec.mean(dim="month")

The weighted sum corresponds to:

.. ipython:: python

weighted_sum = (prec * weights).sum()
weighted_sum

and the weighted mean to:

.. ipython:: python

weighted_mean = weighted_sum / weights.sum()
weighted_mean

However, the functions also take missing values in the data into account:

.. ipython:: python

data = xr.DataArray([np.NaN, 2, 4])
weights = xr.DataArray([8, 1, 1])

data.weighted(weights).mean()

Using ``(data * weights).sum() / weights.sum()`` would (incorrectly) result
in 0.6.


If the weights add up to to 0, ``sum`` returns 0:

.. ipython:: python

data = xr.DataArray([1.0, 1.0])
weights = xr.DataArray([-1.0, 1.0])

data.weighted(weights).sum()

and ``mean`` returns ``NaN``:

.. ipython:: python

data.weighted(weights).mean()


.. note::
``weights`` must be a :py:class:`DataArray` and cannot contain missing values.
Missing values can be replaced manually by ``weights.fillna(0)``.

.. _comput.coarsen:

Coarsen large arrays
====================

``DataArray`` and ``Dataset`` objects include a
:py:class:`DataArray` and :py:class:`Dataset` objects include a
:py:meth:`~xarray.DataArray.coarsen` and :py:meth:`~xarray.Dataset.coarsen`
methods. This supports the block aggregation along multiple dimensions,

Expand Down
1 change: 1 addition & 0 deletions doc/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Examples

examples/weather-data
examples/monthly-means
examples/area_weighted_temperature
examples/multidimensional-coords
examples/visualization_gallery
examples/ROMS_ocean_model
Expand Down
Loading

0 comments on commit 8f464c4

Please sign in to comment.